Does the Yii2 non-DB attribute (or virtual) not populate during mass assignment?

I defined a virtual attribute:

class ContactForm extends Model { public $name; // is not a DB field 

I noticed that it does not fill during mass assignment (after submitting the form to $model->load($_POST) ). Could this be somehow filled with the attributes of the database? Or am I doing something wrong that is not populated, but should it be? Thanks!

0
source share
1 answer

Documents: Massive Jobs

As with regular models, Active Record instances also have a large assignment function. With this function, you can assign values ​​to multiple attributes of an Active Record instance in a single PHP expression, as shown below. Remember that only safe attributes can be assigned in bulk.

Documents: Safe Attributes

For this reason, a special alias safe validator is provided so that you can declare an attribute safe without actually validating it. For example, the following rules declare that both headings and descriptions are safe attributes.

You should do some checking on your attribute, if you do not have any validation requirements - define it as safe .

 public function rules() { return [ [['name'], 'safe'], ]; } 
+2
source

All Articles