Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
21b16ad0
Commit
21b16ad0
authored
Feb 01, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ! usage for default scenario example to model guide
parent
42e4e897
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
3 deletions
+49
-3
model.md
docs/guide/model.md
+49
-3
No files found.
docs/guide/model.md
View file @
21b16ad0
...
...
@@ -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
--------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment