0

動作を使用しようとしていますが、ejsonbehavior正しく起動されていません。

内線: http://www.yiiframework.com/extension/ejsonbehavior/


ejsonbehavior 動作パス:

extensions/behaviors/EJsonBehavior.php

モデル - Users.php:

public function behaviors()
{
    return array(
        'EJsonBehavior' => array('class' => 'ext.behaviors.EJsonBehavior'),
    );
}

Controller.php:

$model = Users::model()->findAll();
echo $model->toJSON();

エラー結果:

Fatal error: Call to a member function toJSON() on a non-object in .../Controller.php on line x
4

1 に答える 1

4

This has nothing to do with the behavior. Your controller function is returning an array:

Users::model()->findAll();

The behavior is attached to every element of the array. So eather you perform toJSON() on every model in the array:

foreach($model as $item) {
  echo $item->toJSON();
}

Or you check the docs for a more appropriate method to obtain just one model in order to perform your echo: http://www.yiiframework.com/doc/api/1.1/CActiveRecord

于 2013-09-26T23:09:44.947 に答える