Yii сохранение массива в атрибутах модели


Я пытаюсь обновить строку, когда пользователь получает ключ аутентификации, но мои атрибуты $model-> остаются пустыми, когда я сохраняю в них свой массив $data. Вот что у меня есть:

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

Что я упускаю из виду?

Author: tereško, 2013-10-03

1 answers

$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

 3
Author: Samuel Liew, 2013-10-03 03:36:53