• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

yii为模型的attributes赋值失败

武飞扬头像
PHP中文网
帮助15

学新通技术网

问题:

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...
    );
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tangbkehb
系列文章
更多 icon
同类精品
更多 icon
继续加载