Difference between #8 and #10 of
Understanding Scenarios

Changes

Title unchanged

Understanding Scenarios

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Form validation, Scenarios, validation rules, model validation, understanding

Content changed

[...]
//Note: the default scenario for CActiveRecord is 'insert' so keep this in mind
//when using these examples
```

CActiveRecord scenarios
 
------------------
 
CActiveRecord sets a number of scenarios internally, any rules you set are validated against these scenarios depending on what operation is performed.
 
 
#### 1. Insert
 
 
All instances of CActiveRecord instantiated via the constructor with the _new_ keyword are set to this scenario by default. Any rules with the scenario will only be validated on database insert (first call to the save() method).
 
 
#### 2. Update
 
All instances of CActiveRecord instantiated via the find() methods are set to this scenario by default. Any rules with the scenario will only be validated on database updates (subsequent calls to the save() method).
 
 
#### 3. Search
 
 
This is not used internally but it is worth a mention as all the Gii generated model and crud code uses the 'search' scenario. The models generated by Gii create a rule like:
 
 
 
```php 
array('id, email, last_ip, created, last_login', 'safe', 'on' => 'search'),
 
```
 
 
This allows the mass assignment of these attributes when the scenario is set to search.
 
Conclusion ---------- Now when you validate user inputs you can stratify or separate different input rules on validation. An example of this is wanting to apply more rigid rules to users then to say internal business logic.
23 0
39 followers
Viewed: 181 253 times
Version: 1.1
Category: Tutorials
Written by: Luke Jurgs
Last updated by: Luke Jurgs
Created on: Oct 28, 2011
Last updated: 11 years ago
Update Article

Revisions

View all history