phpyiiyii-cmodel

Yii saving array to model attributes


I'm trying to update a row when a user gets an authentication key, but my $model->attributes remain empty when I save my $data array to them. Here's what I have:

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>";
        }
    }
}

prints out to

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

what am I overlooking?


Solution

  • $model->attributes = $key; will not work, because $this->findbyAttributes returns a type of CActiveRecord(CModel).

    To copy attributes from one model to another, use the setAttributes() method with the second flag set to false.

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

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