structure-models.md 21.7 KB
Newer Older
Qiang Xue committed
1 2
Models
======
Alexander Makarov committed
3

Qiang Xue committed
4 5
Models are objects representing business data, rules and logic. They are part of
the [MVC](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) architecture.
Qiang Xue committed
6

Qiang Xue committed
7 8
You can create model classes by extending [[yii\base\Model]] or its child classes. The base class
[[yii\base\Model]] supports many useful features:
Larry Ullman committed
9

10 11 12 13 14 15
* [Attributes](#attributes): represent the business data and can be accessed like normal object properties
  or array elements;
* [Attribute labels](#attribute-labels): specify the display labels for attributes;
* [Massive assignment](#massive-assignment): supports populating multiple attributes in a single step;
* [Validation](#validation): ensures input data based on the declared validation rules;
* [Data Exporting](#data-exporting): allows model data to be exported in terms of arrays with customizable formats.
Alexander Makarov committed
16

Qiang Xue committed
17 18
The `Model` class is also the base class for more advanced models, such as [Active Record](db-active-record.md).
Please refer to the relevant documentation for more details about these advanced models.
Alexander Makarov committed
19

Qiang Xue committed
20 21
> Info: You are not required to base your model classes on [[yii\base\Model]]. However, because there are many Yii
  components built to support [[yii\base\Model]], it is usually the preferable base model classes.
Alexander Makarov committed
22

Alexander Makarov committed
23

24
## Attributes <a name="attributes"></a>
Alexander Makarov committed
25

Qiang Xue committed
26 27 28 29
Attributes are the properties that represent business data. By default, attributes are *non-static public*
member variables if your model class extends directly from [[yii\base\Model]].

The following code creates a `ContactForm` model class with four attributes: `name`, `email`, `subject` and `body`.
Alexander Makarov committed
30 31

```php
Qiang Xue committed
32 33 34 35 36 37 38 39 40 41 42
namespace app\models;

use yii\base\Model;

class ContactForm extends Model
{
    public $name;
    public $email;
    public $subject;
    public $body;
}
Alexander Makarov committed
43 44
```

Qiang Xue committed
45
Naturally, you can access an attribute like accessing a normal object property:
Alexander Makarov committed
46 47

```php
Qiang Xue committed
48 49 50
$model = new \app\models\ContactForm;
$model->name = 'example';
echo $model->name;
Alexander Makarov committed
51 52
```

Qiang Xue committed
53 54 55
You can also access attributes like accessing array elements, thanks to the support for
[ArrayAccess](http://php.net/manual/en/class.arrayaccess.php) and [ArrayIterator](http://php.net/manual/en/class.arrayiterator.php)
by [[yii\base\Model]]:
Alexander Makarov committed
56 57

```php
Qiang Xue committed
58 59 60 61 62 63 64 65 66
$model = new \app\models\ContactForm;

// accessing attributes like array elements
$model['name'] = 'example';
echo $model['name'];

// iterate attributes
foreach ($model as $name => $value) {
    echo "$name: $value\n";
Alexander Makarov committed
67 68 69
}
```

70 71 72 73
The method [[yii\base\Model::attributes()]] defines and returns the names of the attributes in a model.
You may override this method to support different ways of defining attributes. For example, [[yii\db\ActiveRecord]]
does so by returning table column names as attribute names. Note that you may also need to override the magic
methods such as `__get()`, `__set()` so that the attributes can be accessed like normal object properties.
Qiang Xue committed
74

Alexander Makarov committed
75

76
## Attribute Labels <a name="attribute-labels"></a>
Alexander Makarov committed
77

Qiang Xue committed
78 79 80
When displaying values or getting input for attributes, you often need to display some labels associated
with attributes. For example, given an attribute named `firstName`, you may want to display a label `First Name`
which is more user-friendly when displayed to end users in places such as form inputs and error messages.
Alexander Makarov committed
81

Qiang Xue committed
82 83 84 85
By default, attribute labels are automatically generated from attribute names. The generation is done by
the method [[yii\base\Model::generateAttributeLabel()]]. It will turn camel-case variable names into
multiple words with the first letter in each word in upper case. For example, `username` becomes `Username`,
and `firstName` becomes `First Name`.
Alexander Makarov committed
86

Qiang Xue committed
87 88
If you do not want to use automatically generated labels, you may override [[yii\base\Model::attributeLabels()]]
to explicitly declare attribute labels. For example,
Alexander Makarov committed
89 90

```php
Qiang Xue committed
91 92 93 94 95
namespace app\models;

use yii\base\Model;

class ContactForm extends Model
Alexander Makarov committed
96
{
Qiang Xue committed
97 98 99 100
    public $name;
    public $email;
    public $subject;
    public $body;
101 102 103 104

    public function attributeLabels()
    {
        return [
Qiang Xue committed
105 106 107 108
            'name' => 'Your name',
            'email' => 'Your email address',
            'subject' => 'Subject',
            'body' => 'Content',
109 110
        ];
    }
Alexander Makarov committed
111 112 113
}
```

Qiang Xue committed
114 115
For applications supporting multiple languages, you may want to translate attribute labels. This can be done
in the [[yii\base\Model::attributeLabels()|attributeLabels()]] method as well, like the following:
Alexander Makarov committed
116

Qiang Xue committed
117 118 119 120 121 122 123 124 125 126 127 128
```php
public function attributeLabels()
{
    return [
        'name' => \Yii::t('app', 'Your name'),
        'email' => \Yii::t('app', 'Your email address'),
        'subject' => \Yii::t('app', 'Subject'),
        'body' => \Yii::t('app', 'Content'),
    ];
}
```

129 130 131
You may even conditionally define attribute labels. For example, based on the [scenario](#scenarios) the model
is being used in, you may return different labels for the same attribute.

Qiang Xue committed
132 133
> Info: Strictly speaking, attribute labels are part of [views](structure-views.md). But declaring labels
  in models is often very convenient and can result in very clean and reusable code.
134

Alexander Makarov committed
135

136
## Scenarios <a name="scenarios"></a>
Alexander Makarov committed
137

Qiang Xue committed
138 139 140 141 142 143
A model may be used in different *scenarios*. For example, a `User` model may be used to collect user login inputs,
but it may also be used for the user registration purpose. In different scenarios, a model may use different
business rules and logic. For example, the `email` attribute may be required during user registration,
but not so during user login.

A model uses the [[yii\base\Model::scenario]] property to keep track of the scenario it is being used in.
144 145 146 147 148 149 150 151 152 153 154
By default, a model supports only a single scenario named `default`. The following code shows two ways of
setting the scenario of a model:

```php
// scenario is set as a property
$model = new User;
$model->scenario = 'login';

// scenario is set through configuration
$model = new User(['scenario' => 'login']);
```
Qiang Xue committed
155

156 157
To support multiple scenarios by a single model, you may override the [[yii\base\Model::scenarios()]] method,
like the following:
Alexander Makarov committed
158 159

```php
Qiang Xue committed
160 161 162 163 164
namespace app\models;

use yii\db\ActiveRecord;

class User extends ActiveRecord
Alexander Makarov committed
165
{
166 167 168 169 170 171 172
    public function scenarios()
    {
        return [
            'login' => ['username', 'password'],
            'register' => ['username', 'email', 'password'],
        ];
    }
Alexander Makarov committed
173 174 175
}
```

Qiang Xue committed
176 177 178 179 180 181 182
> Info: In the above and following examples, the model classes are extending from [[yii\db\ActiveRecord]]
  because the usage of multiple scenarios usually happens to [Active Record](db-active-record.md) classes.

The `scenarios()` method returns an array whose keys are the scenario names and values the corresponding
*active attributes*. An active attribute can be [massively assigned](#massive-assignment) and is subject
to [validation](#validation). In the above example, the `username` and `password` attributes are active
in the `login` scenario; while in the `register` scenario, `email` is also active besides `username` and `password`.
Alexander Makarov committed
183

Qiang Xue committed
184 185 186
The default implementation of `scenarios()` will return all scenarios found in the validation rule declaration
method [[yii\base\Model::rules()]]. When overriding `scenarios()`, if you want to introduce new scenarios
in addition to the default ones, you may write code like the following:
187

188
```php
Qiang Xue committed
189 190 191 192 193
namespace app\models;

use yii\db\ActiveRecord;

class User extends ActiveRecord
194
{
195 196 197 198 199 200 201
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['login'] = ['username', 'password'];
        $scenarios['register'] = ['username', 'email', 'password'];
        return $scenarios;
    }
202 203 204
}
```

Qiang Xue committed
205 206 207
The scenario feature is primarily used by [validation](#validation) and [massive attribute assignment](#massive-assignment).
You can, however, use it for other purposes. For example, you may declare [attribute labels](#attribute-labels)
differently based on the current scenario.
208

209

Qiang Xue committed
210
## Validation <a name="validation"></a>
Alexander Makarov committed
211

212 213 214 215 216 217 218 219 220
When the data for a model is received from end users, it should be validated to make sure it satisfies
certain rules (called *validation rules*, also known as *business rules*). For example, given a `ContactForm` model,
you may want to make sure all attributes are not empty and the `email` attribute contains a valid email address.
If the values for some attributes do not satisfy the corresponding business rules, appropriate error messages
should be displayed to help the user to fix the errors.

You may call [[yii\base\Model::validate()]] to trigger validation. The method will go through every *active rule*
and make sure it is satisfied. If not, an error message will be generated for each failed rule and attribute.
The method returns a boolean value indicating whether all rules are satisfied. If not, you may retrieve the
Qiang Xue committed
221
error messages through the property [[yii\base\Model::errors]] or [[yii\base\Model::firstErrors]]. For example,
Alexander Makarov committed
222 223

```php
224 225 226 227 228
$model = new \app\models\ContactForm;

// populate model attributes with user inputs
$model->attributes = \Yii::$app->request->post('ContactForm');

Alexander Makarov committed
229
if ($model->validate()) {
230
    // all inputs are valid
Alexander Makarov committed
231
} else {
232 233
    // validation failed: $errors is an array containing error messages
    $errors = $model->errors;
Alexander Makarov committed
234 235 236 237
}
```


238 239 240
To declare validation rules associated with a model, override the [[yii\base\Model::rules()]] method by returning
the rules that the model data should satisfy. The following example should the validation rules
for the `ContactForm` model:
241 242 243 244

```php
public function rules()
{
245
    return [
246 247 248 249 250
        // the name, email, subject and body attributes are required
        [['name', 'email', 'subject', 'body'], 'required'],

        // the email attribute should be a valid email address
        ['email', 'email'],
251
    ];
252 253 254
}
```

255
The `rules()` method returns an array of rules, each of which is an array in the following format:
256 257

```php
258 259 260 261 262
[
    // required, specifies which attributes should be validated by this rule.
    // For single attribute, you can use the attribute name directly
    // without having it in an array instead of an array
    ['attribute1', 'attribute2', ...],
263

264 265 266
    // required, specifies the type of this rule.
    // It can be a class name, validator alias, or a validation method name
    'validator',
267

268 269 270
    // optional, specifies in which scenario(s) this rule should be applied
    // if not given, it means the rule applies to all scenarios
    'on' => ['scenario1', 'scenario2', ...],
271

272 273 274
    // optional, specifies additional configurations for the validator object
    'property1' => 'value1', 'property2' => 'value2', ...
]
275 276
```

277 278
A rule may be applied to one or multiple attributes. A rule may be applicable only in certain [scenarios](#scenarios).
When a rule is applicable in a scenario, it is called an *active rule* in that scenario.
279

280
When the `validate()` method is called, it does the following steps to perform validation:
281

282 283 284 285 286
1. Determine which attributes should be validated by checking the current [[yii\base\Model::scenario|scenario]]
   against the scenarios declared in [[yii\base\Model::scenarios()]]. These attributes are the active attributes.
2. Determine which rules should be applied by checking the current [[yii\base\Model::scenario|scenario]]
   against the rules declared in [[yii\base\Model::rules()]]. These rules are the active rules.
3. Use each active rule to validate each active attribute which is associated with the rule.
Evgeniy Tkachenko committed
287

288 289 290
According to the above validation steps, an attribute will be validated if and only if it is
an active attribute declared in `scenarios()` and it is associated with one or multiple active rules
declared in `rules()`.
Evgeniy Tkachenko committed
291

292 293 294 295
Yii provides a set of built-in validators to support commonly needed data validation tasks. You may also
create your own validators by extending [[yii\validators\Validator]] or writing an inline validation method
within model classes. For more details about the built-in validators and how to create your own validators,
please refer to the [Input Validation](input-validation.md) section.
296

297 298
> Note: As a rule of thumb, never trust the data coming from end users and always validate them before
  putting them to some good use.
Alexander Makarov committed
299 300


301
## Massive Assignment <a name="massive-assignment"></a>
Alexander Makarov committed
302

303 304 305 306 307
Massive assignment is a convenient way of populating a model with user inputs using a single line of code.
It populates the attributes of a model by assigning the input data directly to the [[yii\base\Model::attributes]]
property. The following two pieces of code are equivalent, both trying to assign the form data submitted by end users
to the attributes of the `ContactForm` model. Clearly, the former, which uses massive assignment, is much cleaner
and less error prone than the latter:
Alexander Makarov committed
308 309

```php
310 311
$model = new \app\models\ContactForm;
$model->attributes = \Yii::$app->request->post('ContactForm');
Alexander Makarov committed
312 313
```

314 315 316 317 318 319 320 321
```php
$model = new \app\models\ContactForm;
$data = \Yii::$app->request->post('ContactForm', []);
$model->name = isset($data['name']) ? $data['name'] : null;
$model->email = isset($data['email']) ? $data['email'] : null;
$model->subject = isset($data['subject']) ? $data['subject'] : null;
$model->body = isset($data['body']) ? $data['body'] : null;
```
Alexander Makarov committed
322

Carsten Brandt committed
323

324
### Safe Attributes <a name="safe-attributes"></a>
Qiang Xue committed
325

326 327 328 329 330
Massive assignment only applies to the so-called *safe attributes* which are the attributes listed in
[[yii\base\Model::scenarios()]] for the current [[yii\base\Model::scenario|scenario]] of a model.
For example, if the `User` model has the following scenario declaration, then when the current scenario
is `login`, only the `username` and `password` can be massively assigned. Any other attributes will
be kept untouched.
Qiang Xue committed
331 332

```php
333 334 335 336 337 338 339
public function scenarios()
{
    return [
        'login' => ['username', 'password'],
        'register' => ['username', 'email', 'password'],
    ];
}
Qiang Xue committed
340 341
```

342 343 344 345
> Info: The reason that massive assignment only applies to safe attributes is because you want to
  control which attributes can be modified by end user data. For example, if the `User` model
  has a `permission` attribute which determines the permission assigned to the user, you would
  like this attribute to be modifiable by administrators through a backend interface only.
Qiang Xue committed
346

347 348 349 350 351 352 353
Because the default implementation of [[yii\base\Model::scenarios()]] will return all scenarios and attributes
found in [[yii\base\Model::rules()]], if you do not override this method, it means an attribute is safe as long
as it appears in one of the active validation rules.

For this reason, a special validator aliased `safe` is provided so that you can declare an attribute
to be safe without actually validating it. For example, the following rules declare that both `title`
and `description` are safe attributes.
Qiang Xue committed
354 355

```php
356
public function rules()
Qiang Xue committed
357
{
358 359 360
    return [
        [['title', 'description'], 'safe'],
    ];
Qiang Xue committed
361 362 363 364
}
```


365
### Unsafe Attributes <a name="unsafe-attributes"></a>
Qiang Xue committed
366

367 368 369 370
As described above, the [[yii\base\Model::scenarios()]] method serves for two purposes: determining which attributes
should be validated, and determining which attributes are safe. In some rare cases, you may want to validate
an attribute but do not want to mark it safe. You can do so by prefixing an exclamation mark `!` to the attribute
name when declaring it in `scenarios()`, like the `secret` attribute in the following:
Alexander Makarov committed
371 372

```php
373
public function scenarios()
Alexander Makarov committed
374
{
375 376 377
    return [
        'login' => ['username', 'password', '!secret'],
    ];
Alexander Makarov committed
378
}
Alexander Makarov committed
379 380
```

381 382 383
When the model is in the `login` scenario, all three attributes will be validated. However, only the `username`
and `password` attributes can be massively assigned. To assign an input value to the `secret` attribute, you
have to do it explicitly as follows,
Alexander Makarov committed
384 385

```php
386
$model->secret = $secret;
Alexander Makarov committed
387 388 389
```


390 391 392 393 394 395 396
## Data Exporting <a name="data-exporting"></a>

Models often need to be exported in different formats. For example, you may want to convert a collection of
models into JSON or Excel format. The exporting process can be broken down into two independent steps.
In the first step, models are converted into arrays; in the second step, the arrays are converted into
target formats. You may just focus on the first step, because the second step can be achieved by generic
data formatters, such as [[yii\web\JsonResponseFormatter]].
Alexander Makarov committed
397

398 399
The simplest way of converting a model into an array is to use the [[yii\base\Model::attributes]] property.
For example,
Alexander Makarov committed
400 401

```php
402 403
$post = \app\models\Post::findOne(100);
$array = $post->attributes;
Alexander Makarov committed
404 405
```

406 407
By default, the [[yii\base\Model::attributes]] property will return the values of *all* attributes
declared in [[yii\base\Model::attributes()]].
Alexander Makarov committed
408

409 410 411 412 413
A more flexible and powerful way of converting a model into an array is to use the [[yii\base\Model::toArray()]]
method. Its default behavior is the same as that of [[yii\base\Model::attributes]]. However, it allows you
to choose which data items, called *fields*, to be put in the resulting array and how they should be formatted.
In fact, it is the default way of exporting models in RESTful Web service development, as described in
the [Response Formatting](rest-response-formatting.md).
Alexander Makarov committed
414

Alexander Makarov committed
415

416
### Fields <a name="fields"></a>
Alexander Makarov committed
417

418 419 420 421 422 423 424 425 426 427
A field is simply a named element in the array that is obtained by calling the [[yii\base\Model::toArray()]] method
of a model.

By default, field names are equivalent to attribute names. However, you can change this behavior by overriding
the [[yii\base\Model::fields()|fields()]] and/or [[yii\base\Model::extraFields()|extraFields()]] methods. Both methods
should return a list of field definitions. The fields defined by `fields()` are default fields, meaning that
`toArray()` will return these fields by default. The `extraFields()` method defines additionally available fields
which can also be returned by `toArray()` as long as you specify them via the `$expand` parameter. For example,
the following code will return all fields defined in `fields()` and the `prettyName` and `fullAddress` fields
if they are defined in `extraFields()`.
Alexander Makarov committed
428

Alexander Makarov committed
429
```php
430
$array = $model->toArray([], ['prettyName', 'fullAddress']);
Alexander Makarov committed
431
```
Carsten Brandt committed
432

433 434 435 436 437
You can override `fields()` to add, remove, rename or redefine fields. The return value of `fields()`
should be an array. The array keys are the field names, and the array values are the corresponding
field definitions which can be either property/attribute names or anonymous functions returning the
corresponding field values. In the special case when a field name is the same as its defining attribute
name, you can omit the array key. For example,
438 439

```php
440 441 442
// explicitly list every field, best used when you want to make sure the changes
// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).
public function fields()
443
{
444 445 446
    return [
        // field name is the same as the attribute name
        'id',
447

448 449
        // field name is "email", the corresponding attribute name is "email_address"
        'email' => 'email_address',
450

451 452 453 454 455 456
        // field name is "name", its value is defined by a PHP callback
        'name' => function () {
            return $this->first_name . ' ' . $this->last_name;
        },
    ];
}
457

458 459 460 461 462
// filter out some fields, best used when you want to inherit the parent implementation
// and blacklist some sensitive fields.
public function fields()
{
    $fields = parent::fields();
463

464 465
    // remove fields that contain sensitive information
    unset($fields['auth_key'], $fields['password_hash'], $fields['password_reset_token']);
466

467 468
    return $fields;
}
469 470
```

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
> Warning: Because by default all attributes of a model will be included in the exported array, you should
> examine your data to make sure they do not contain sensitive information. If there is such information,
> you should override `fields()` to filter them out. In the above example, we choose
> to filter out `auth_key`, `password_hash` and `password_reset_token`.


## Best Practices <a name="best-practices"></a>

Models are the central places to represent business data, rules and logic. They often need to be reused
in different places. In a well-designed application, models are usually much fatter than
[controllers](structure-controllers.md).

In summary, models

* may contain attributes to represent business data;
* may contain validation rules to ensure the data validity and integrity;
* may contain methods implementing business logic;
* should NOT directly access request, session, or any other environmental data. These data should be injected
  by [controllers](structure-controllers.md) into models;
* should avoid embedding HTML or other presentational code - this is better done in [views](structure-views.md);
* avoid having too many [scenarios](#scenarios) in a single model.

You may usually consider the last recommendation above when you are developing large complex systems.
In these systems, models could be very fat because they are used in many places and may thus contain many sets
of rules and business logic. This often ends up in a nightmare in maintaining the model code
because a single touch of the code could affect several different places. To make the mode code more maintainable,
you may take the following strategy:

* Define a set of base model classes that are shared by different [applications](structure-applications.md) or
  [modules](structure-modules.md). These model classes should contain minimal sets of rules and logic that
  are common among all their usages.
* In each [application](structure-applications.md) or [module](structure-modules.md) that uses a model,
  define a crete model class by extending from the corresponding base model class. The concrete model classes
  should contain rules and logic that are specific for that application or module.

For example, in the [Advanced Application Template](tutorial-advanced-app.md), you may define a base model
class `common\models\Post`. Then for the front end application, you define and use a concrete model class
`frontend\models\Post` which extends from `common\models\Post`. And similarly for the back end application,
you define `backend\models\Post`. With this strategy, you will be sure that the code in `frontend\models\Post`
is only specific to the front end application, and if you make any change to it, you do not need to worry if
the change may break the back end application.