Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii组件属性在使用getAttributes时未调用get_Php_Model_Yii - Fatal编程技术网

Php Yii组件属性在使用getAttributes时未调用get

Php Yii组件属性在使用getAttributes时未调用get,php,model,yii,Php,Model,Yii,我有一个模型,上面有一张照片。每次初始化/获取此模型时(有一个函数生成它),都应该预先计算此值 我将此“默认值”放入属性的getter中: public function getCid(){ if ($this->_cid == null ){ $this->_cid = generateCid(); } return $this->_cid; } 当我调用它时,它的get方法正常工作: $model->cid; //return

我有一个模型,上面有一张照片。每次初始化/获取此模型时(有一个函数生成它),都应该预先计算此值

我将此“默认值”放入属性的getter中:

public function getCid(){
    if ($this->_cid == null ){
        $this->_cid = generateCid();
    }
    return $this->_cid;
}
当我调用它时,它的get方法正常工作:

$model->cid; //returns good value
但是,当我尝试使用其他参数通过
getAttributes()
函数获取它时,它不会调用它的get方法

$model->getAttributes(array('cid')); //just to try only this property.
我不想在数组中耍花招,用
$model->cid
独立获取它的值,并将其合并到
getAttributes
返回数组中,因为我的应用程序中会有更多属性,我正在寻找一个快速的解决方案

那么我应该把这个生成器移到哪里,或者我应该做些什么来轻松地得到这个生成的id呢

更新:

我创建了一个基本ActiveRecord类,它扩展了
CActiveRecord
,并添加了以下函数:

public function getAttributes($names=true){
    $base = parent::getAttributes($names);
    foreach($base as $key => $value)
        if(!$this->hasAttribute($key))
            $base[$key] = $this[$key];

    return $base;
}

这将调用每个添加属性的get方法,因此它将更新其内容。

您的实际模型是
CActiveRecord
模型吗

根据显示为的一部分,根据您的模型类型,将有不同的处理方法

但是处理它的最快方法可能是重写模型中的
getAttributes
,如果在传入的数组中找到
'cid'
值,则让它对getter执行自定义调用


您可能需要查看想法,覆盖它,然后从自定义覆盖中调用您的
父::getAttributes()

我尝试了类似的方法,但无法运行,因为此方法使用了受保护的
\u attributes
变量。我的解决方案是,循环遍历每个属性,并调用非真实属性。我在另一个答案中附加了我的解决方案,这样每个人都可以很好地看到语法:)但是谢谢,它是基于您的想法。