Properties in the model can be accessed as normal object properties $model->attribute; or accessed as an array element $model[attribute].
Property labels
Get Property Label
$model = new \app\models\contactform;
Display as "Name"
echo $model->getattributelabel (' name ');
Scene
To set up a scene for a model, there are two ways
Scene as a property to set
$model = new User;
$model->scenario = ' login ';
The scenario is set by constructing the initialization configuration
$model = new User ([' Scenario ' = ' login ']);
You can set up multiple scenarios for the model, such as: Set up login and registration for the user model two application scenarios
Sometimes you want a rule to apply only in a certain scenario, so you can specify the properties of the rule on
as follows:
Public Function Rules ()
{
return [
In the "register" scenario username, email and password must have a value
[[' username ', ' email ', ' password '], ' required ', ' on ' = ' register '],
Username and password must have a value in the "login" scenario
[[' Username ', ' password '], ' required ', ' on ' = ' login '],
];
}
Block Assignment value
$model = new \app\models\contactform;
$model->attributes = \yii:: $app->request->post (' contactform ');
or write it in the controller.
$model = new Contactform ();
$model->load (Yii:: $app->request->post ();
Security properties
The properties listed in the scene are security properties, and only the security attributes can be assigned by the block.
Non-security attributes
The [[Yii\base\model::scenarios ()]] method provides two uses: defines which properties should be validated and which properties are safe to define. In some cases, you may want to verify a property but do not want to make it safe, you can add an exclamation mark to the property name in the scenarios () Method!. For example, the Secret property is as follows.
Public Function Scenarios ()
{
return [
' Login ' = [' username ', ' password ', '!secret '],
];
}
When the model is in the login
scene, three properties are validated, but only the and attributes are assigned to the block, and username
password
to secret
assign a value to the property, it must be explicitly assigned to it as in the following example.
$model->secret = $secret;
Property validation Rules Method
Public Function Rules ()
{
return [
[[' title ', ' description '], ' safe '],//These two properties do not need to be verified and will be filtered when validating
];
}
Data export
Models are typically exported to different formats, you might want to convert a collection of models into JSON or Excel, and the export process can be decomposed into two steps, the first step, the model conversion to an array, and the second step, the arrays are converted to the desired format. You only need to focus on the first step, because the second step can be accomplished by a universal data converter such as [[Yii\web\jsonresponseformatter]].
The simplest way to convert a model into an array attributes property $arr = $model->attributes
The more flexible and powerful way is through the ToArray () method, which is the way to implement restful styles. $model->toarray ()
About several concepts or points of knowledge in a model