关于Yii框架中不能直接给模型的attributes赋值的解决办法

先看一下源代码:
$menuId = isset($_GET['mId']) ? $_GET['mId'] : 0;
if ($menuId) {
    $menu = MenuTree::model()->findByPk($menuId);
    if(isset($_POST['MenuTree'])){
        var_dump($menu->attributes); //在这里跟踪输出的数据正常,跟表单中填写的一致
        $menu->attributes = $_POST['MenuTree']; //对attributes进行赋值
        var_dump($menu->attributes); //输出$menu模型中的attributes,不正常,结果并不是POST接收到的值,而是数据库原有的值
        if($menu->save()){
            Yii::app()->user->setFlash('success',"恭喜您,修改成功,请继续!");
            $this->redirect(Yii::app()->createUrl('menu/contentEdit',array('mId'=>$menuId)));
        }else{
            throw new CException("修改失败!");
        }
    }
    $this->render("contentEdit", array('menu' => $menu));
} else {
    throw new CHttpException('404');
}
这是一个页面的action代码,看代码中的注释可以看到出现的错误,死活不接受POST的赋值,试过使用setAttributes函数也一样不接受,输出的还是原数据库的值,但是用updateByPk操作就可以,跟踪了一下setAttributes函数,发现函数定义如下:

public function setAttributes($values,$safeOnly=true)
{
    if(!is_array($values))
        return;
    $attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
    foreach($values as $name=>$value)
    {
        if(isset($attributes[$name]))
            $this->$name=$value;
        else if($safeOnly)
            $this->onUnsafeAttribute($name,$value);
    }
}

分析setAttributes函数代码可以看到,在对attributes赋值时进行的安全检查,所以想到原因可能出现模型的rules没有对那几个表单中修改的字段设置为安全,找到原因,解决方案就出来了,在model的rules中,把想要修改的字段的属性置为安全即可。


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        // more code...
        array('field1 , field2 ,field3', 'safe'), //Modify the fields in here
        // more code...
    );
}


你可能感兴趣的:(PHP)