2013-10-02 49 views
2

我试图在用户获取身份验证密钥时更新行,但是当我将$ data数据保存到他们时,我的$ model->属性保持为空。下面是我有:Yii将数组保存到模型属性

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] => 
) 

我是什么俯瞰?

回答

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