我正在尝试使用yii,我遵循一个教程来简单创建一个数据库表的鞋场的页面。我创建索引视图
<?php
/* @var $this yiiwebView */
?>
<h1>articoli/index</h1>
<p>
pippo
<?php
foreach($posts as $post){?>
<h1><?php echo $post->autore; ?> </h1>
<p><?php echo $post->articolo; ?></p>
}
?>
</p>
在控制器中我创建Articolicontroller
<?php
namespace appcontrollers;
class ArticoliController extends yiiwebController
{
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);
}
}
在模型中创建Articoli .php
<?php
namespace appmodels;
use Yii;
/**
* This is the model class for table "articoli".
*
* @property integer $id
* @property string $autore
* @property string $articolo
*/
class Articoli extends yiidbActiveRecord
{
/**
* @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',
];
}
}
当我尝试返回时
PHP Fatal Error – yiibaseErrorException
Class 'appcontrollersArticoli' not found
我不明白。我认为它必须转到app models articoli.php
我尝试不同的方式$ posts = articoli :: -> findall();
但不工作
yii2 activerecord没有静态函数 model()
。要获取Articoli
的所有记录,您必须使用findAll()
静态方法或find()->all()
。
将控制器中的用法更改为:
$posts = Articoli::findAll();
在您的控制器中添加use
:
use appmodelsArticoli;
或只是更改此行:
$posts=Articoli::model()->findall();
:
$posts = appmodelsArticoli::findAll();
仅此而已!;)