2017-09-21 2 views
0

Ich versuche, yii zu verwenden, und ich folge einem Tutorial, um einfach eine Seite zu erstellen, die Felder der Datenbank Tabelle schuhen. i create index Ansichtyii Fehler aktive Datensatzabfrage

<?php 
/* @var $this yii\web\View */ 
?> 
<h1>articoli/index</h1> 

<p> 
    pippo 

    <?php 
    foreach($posts as $post){?> 
     <h1><?php echo $post->autore; ?> </h1> 
     <p><?php echo $post->articolo; ?></p> 

    } 
    ?> 
</p> 

in Controller erstellen i ArticoliController

<?php 

namespace app\controllers; 

class ArticoliController extends \yii\web\Controller 
{ 
    public function actionIndex() 
    { 
     $posts=Articoli::model()->findall(); 
     $data['posts']=$posts; 
     return $this->render('index',$data); 
    } 

    public function actionSaluta(){ 

     $vsa['messaggio']='Alessio'; 
     return $this->render('saluta',$vsa); 
    } 

} 

in Modell erstellen i Articoli .php

<?php 

namespace app\models; 

use Yii; 

/** 
* This is the model class for table "articoli". 
* 
* @property integer $id 
* @property string $autore 
* @property string $articolo 
*/ 
class Articoli extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'articoli'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['articolo'], 'required'], 
      [['autore'], 'string', 'max' => 55], 
      [['articolo'], 'string', 'max' => 255], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'autore' => 'Autore', 
      'articolo' => 'Articolo', 
     ]; 
    } 
} 

, wenn ich es zurückgeben versuchen

PHP Fatal Error – yii\base\ErrorException 

Class 'app\controllers\Articoli' not found 

ich verstehe nicht. Ich denke, dass es zu app \ models \ Articoli.php gehen muss

Ich versuche es anders $ posts = Articoli :: -> findall();

aber nicht funktionieren

+0

Schauen Sie sich diese Anleitung an: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findAll()-detail –

Antwort

1

Yii2 Active haben keine statische Funktion model(). Um alle Datensätze von Articoli abzurufen, müssen Sie die statische Methode findAll() oder find()->all() verwenden.

ändern Verwendung in der Steuerung an:

$posts = Articoli::findAll(); 

In Ihrem Controller hinzufügen use:

use \app\models\Articoli; 


Oder nur diese Zeile ändern:

$posts=Articoli::model()->findall(); 

dazu:

$posts = \app\models\Articoli::findAll(); 

Und das ist alles! ;)

Verwandte Themen