2

当用户获得身份验证密钥时,我试图更新一行,但是当我将 $data 数组保存到他们时,我的 $model->attributes 仍然为空。这是我所拥有的:

public function redeemKey($key,$subscription_id){
    $key = $this->findbyAttributes(array('key'=>$key));
    if(count($key) === 1){
        if(is_null($key['date_used'])){
            $model = new NewsItemPass;
            $model->attributes = $key;
            echo "<pre>",print_r($model->attributes),"</pre>";
        }
    }
}

打印到

Array
(
    [id] => 
    [key] => 
    [date_created] => 
    [date_used] => 
    [date_expired] => 
    [news_subscription_id] => 
    [duration] => 
)

我在看什么?

4

1 回答 1

3

$model->attributes = $key;将不起作用,因为$this->findbyAttributes返回一种 CActiveRecord(CModel)。

要将属性从一个模型复制到另一个模型,请使用setAttributes()将第二个标志设置为 false 的方法。

$model->setAttributes($key->attributes, false);

http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail

于 2013-10-03T03:36:53.177 回答