【发布时间】:2012-12-17 15:16:08
【问题描述】:
我正在调用findAll 方法,我得到4 个字段。我现在想再添加一个名为$owned 的字段。
所以这意味着在我从表中获取记录后,结果数据记录应该包含拥有的字段。
此外,$owned 字段是动态的,取决于用户是否是组的所有者。我尝试使用afterFind。但它也不起作用。令人惊讶的是,它将属性$owned 添加到对象而不是属性。
我在控制器中使用CJSON::encode($model) 来查看输出。$owned 字段未显示。
下面是代码
/**
*
* The followings are the available columns in table 'group':
* @property integer $id
* @property string $name
* @property string $created_at
* @property string $updated_at
*/
class Group extends CActiveRecord
{
//adding owned property for groups.true if user is owner
public $owned;
protected function afterFind()
{
parent::afterFind();
//if user is owner of group its true
$this->owned = true;
}
【问题讨论】:
-
您可以在您的问题中发布您的整个
Group模型吗?您是否通过在rules()方法中声明owned将其定义为属性? -
你可以尝试在分配后打电话给
parent::afterFind()(不确定它会改变什么,但你可以试一试)
标签: activerecord yii