From 21b16ad06b4c0aff454c87857083d6caad7eacf5 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 1 Feb 2014 21:03:58 +0400 Subject: [PATCH] Added ! usage for default scenario example to model guide --- docs/guide/model.md | 52 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/guide/model.md b/docs/guide/model.md index ff6a29acae..29dc73463e 100644 --- a/docs/guide/model.md +++ b/docs/guide/model.md @@ -265,7 +265,7 @@ assignment is described in `scenarios` method: ```php class User extends ActiveRecord { - function rules() + public function rules() { return [ // rule applied when corresponding field is "safe" @@ -278,7 +278,7 @@ class User extends ActiveRecord ]; } - function scenarios() + public function scenarios() { return [ // on signup allow mass assignment of username @@ -328,7 +328,7 @@ In case of not defined `scenarios` method like the following: ```php class User extends ActiveRecord { - function rules() + public function rules() { return [ ['username', 'string', 'length' => [4, 32]], @@ -363,6 +363,52 @@ array( ) ``` +If you want some methods to be unsafe for default scenario: + +```php +class User extends ActiveRecord +{ + function rules() + { + return [ + ['username', 'string', 'length' => [4, 32]], + ['first_name', 'string', 'max' => 128], + ['password', 'required'], + ]; + } + + public function scenarios() + { + return [ + self::DEFAULT_SCENARIO => ['username', 'first_name', '!password'] + ]; + } +} +``` + +Mass assignment is still available by default: + +```php +$user = User::find(42); +$data = [ + 'username' => 'samdark', + 'first_name' => 'Alexander', + 'password' => '123', +]; +$user->attributes = $data; +print_r($data); +``` + +The code above gives you: + +```php +array( + 'username' => 'samdark', + 'first_name' => 'Alexander', + 'password' => null, // because of ! before field name in scenarios +) +``` + See also --------