【问题标题】:Yii saving array to model attributesYii 将数组保存到模型属性
【发布时间】:2013-10-02 23:18:33
【问题描述】:

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

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

我忽略了什么?

【问题讨论】:

    标签: php yii yii-cmodel


    【解决方案1】:

    $model-&gt;attributes = $key; 将不起作用,因为$this-&gt;findbyAttributes 返回的类型为 CActiveRecord(CModel)。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-25
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多