Yii2: Yii Cookbook Fixes

Created on 23 May 2018  Â·  3Comments  Â·  Source: yiisoft/yii2

The example for using Yii with the third party includes error on the last script

// legacy code
$id = (int)$_GET['id'];

// new code
require 'path/to/yii_init.php';
$post = \app\models\Post::find()->where['id' => $id];
echo Html::encode($post->title);

just fix the line $post = \app\models\Post::find()->where['id' => $id]; to $post = \app\models\Post::find()->where(['id' => $id]);

docs

All 3 comments

Thanks! Yii2 Cookbook is in a separate repository - https://github.com/samdark/yii2-cookbook.

Sorry, @arogachev didn't know about the separate repo, will add there next time.

Also you should add the ->all() or ->one() on the end of the statement $post = \app\models\Post::find()->where(['id' => $id]); , because the very next line calls the

echo Html::encode($post->title);

which throws error

Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: yii\db\ActiveQuery::name

@buttflattery Good catch! Updated the PR with this fix. Thanks!

Was this page helpful?
0 / 5 - 0 ratings