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'); }
(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); } }
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... ); }
For more programming-related content, please pay attention to the Programming Introduction column 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

How to use Attributes to manage code annotations in PHP8? With the release of PHP8, an exciting new feature was introduced, namely Attributes. Attributes is a method of code annotation that allows us to add metadata to classes, methods, and attributes in a structured way. In this article, we will explore how to use Attributes to manage code annotations in PHP8 and provide some specific code examples. 1. What is Attrib?

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

How to use Attributes to add custom annotations to classes in PHP8? Custom annotations are a way to add metadata to a class or method, which can help us obtain and process additional information on a specific class or method at runtime. In PHP8, the concept of Attributes was introduced, which allows us to easily add custom annotations to classes. This article will introduce how to use Attributes to implement custom annotations for classes in PHP8 and provide specific code examples. In PHP8, since

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands
