Reputation: 99
i'm trying to use yii and i follow a tutorial to simple create a page that shoe fields of database's table. i create index view
<?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 controllers i create 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 model i create 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',
];
}
}
when i try it return
PHP Fatal Error – yii\base\ErrorException
Class 'app\controllers\Articoli' not found
i don't understand. I think that it must go to app\models\Articoli.php
I try different way $posts=Articoli::->findall();
but don't work
Upvotes: 0
Views: 133
Reputation: 5032
Yii2 ActiveRecord don't have static function model()
. To fetch all records from Articoli
you have to use findAll()
static method, or find()->all()
.
Change usage in controller to:
$posts = Articoli::findAll();
In your controller add use
:
use \app\models\Articoli;
Or just change this line:
$posts=Articoli::model()->findall();
to this:
$posts = \app\models\Articoli::findAll();
And that's all! ;)
Upvotes: 1