Home> PHP Framework> YII> body text

Failed to assign values to model attributes in Yii

王林
Release: 2020-02-26 17:14:24
Original
3068 people have browsed it

Failed to assign values ​​to model attributes in Yii

Problem:

You cannot directly assign values to model attributes in the Yii framework.

First take a look at the source code:

$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'); }
Copy after login

(Related article tutorials recommended:yii framework)

This is the action code of a page, Looking at the comments in the code, you can see the error that occurred. The POST assignment is not accepted. I tried using the setAttributes function and it was also not accepted. The output is still the value of the original database, but it can be operated with updateByPk. After tracking the setAttributes function, I found that The function definition is as follows:

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); } }
Copy after login

Analyzing the setAttributes function code, we can see that security checks are performed when assigning values to attributes, so I think the reason may be that the rules of the model are not set to be safe for the fields modified in those forms. After finding the reason, the solution comes out. In the rules of the model, just set the attribute of the field you want to modify to safe.

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

For more programming-related content, please pay attention to theProgramming Introductioncolumn on the php Chinese website!

The above is the detailed content of Failed to assign values to model attributes in Yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!