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
039ed688
Commit
039ed688
authored
Nov 24, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1301: fixed scenario generation when there are "except" scenarios.
parent
337b1b08
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
13 deletions
+70
-13
Model.php
framework/yii/base/Model.php
+32
-13
ModelTest.php
tests/unit/framework/base/ModelTest.php
+38
-0
No files found.
framework/yii/base/Model.php
View file @
039ed688
...
...
@@ -165,7 +165,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* ]
* ~~~
*
* By default, an active attribute
that
is considered safe and can be massively assigned.
* By default, an active attribute is considered safe and can be massively assigned.
* If an attribute should NOT be massively assigned (thus considered unsafe),
* please prefix the attribute with an exclamation character (e.g. '!rank').
*
...
...
@@ -178,29 +178,48 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
*/
public
function
scenarios
()
{
$scenarios
=
[];
$defaults
=
[];
/** @var $validator Validator */
$scenarios
=
[
self
::
DEFAULT_SCENARIO
=>
[]];
foreach
(
$this
->
getValidators
()
as
$validator
)
{
if
(
empty
(
$validator
->
on
))
{
foreach
(
$validator
->
attributes
as
$attribute
)
{
$defaults
[
$attribute
]
=
true
;
foreach
(
$validator
->
on
as
$scenario
)
{
$scenarios
[
$scenario
]
=
[];
}
foreach
(
$validator
->
except
as
$scenario
)
{
$scenarios
[
$scenario
]
=
[];
}
}
$names
=
array_keys
(
$scenarios
);
foreach
(
$this
->
getValidators
()
as
$validator
)
{
if
(
empty
(
$validator
->
on
)
&&
empty
(
$validator
->
except
))
{
foreach
(
$names
as
$name
)
{
foreach
(
$validator
->
attributes
as
$attribute
)
{
$scenarios
[
$name
][
$attribute
]
=
true
;
}
}
}
elseif
(
empty
(
$validator
->
on
))
{
foreach
(
$names
as
$name
)
{
if
(
!
in_array
(
$name
,
$validator
->
except
,
true
))
{
foreach
(
$validator
->
attributes
as
$attribute
)
{
$scenarios
[
$name
][
$attribute
]
=
true
;
}
}
}
}
else
{
foreach
(
$validator
->
on
as
$
scenario
)
{
foreach
(
$validator
->
on
as
$
name
)
{
foreach
(
$validator
->
attributes
as
$attribute
)
{
$scenarios
[
$
scenario
][
$attribute
]
=
true
;
$scenarios
[
$
name
][
$attribute
]
=
true
;
}
}
}
}
foreach
(
$scenarios
as
$scenario
=>
$attributes
)
{
foreach
(
array_keys
(
$defaults
)
as
$attribute
)
{
$attributes
[
$attribute
]
=
true
;
if
(
empty
(
$attributes
))
{
unset
(
$scenarios
[
$scenario
]);
}
else
{
$scenarios
[
$scenario
]
=
array_keys
(
$attributes
);
}
$scenarios
[
$scenario
]
=
array_keys
(
$attributes
);
}
$scenarios
[
self
::
DEFAULT_SCENARIO
]
=
array_keys
(
$defaults
);
return
$scenarios
;
}
...
...
tests/unit/framework/base/ModelTest.php
View file @
039ed688
...
...
@@ -217,6 +217,20 @@ class ModelTest extends TestCase
{
$singer
=
new
Singer
();
$this
->
assertEquals
([
'default'
=>
[
'lastName'
,
'underscore_style'
]],
$singer
->
scenarios
());
$scenarios
=
[
'default'
=>
[
'id'
,
'name'
,
'description'
],
'administration'
=>
[
'name'
,
'description'
,
'is_disabled'
],
];
$model
=
new
ComplexModel1
();
$this
->
assertEquals
(
$scenarios
,
$model
->
scenarios
());
$scenarios
=
[
'default'
=>
[
'id'
,
'name'
,
'description'
],
'suddenlyUnexpectedScenario'
=>
[
'name'
,
'description'
],
'administration'
=>
[
'id'
,
'name'
,
'description'
,
'is_disabled'
],
];
$model
=
new
ComplexModel2
();
$this
->
assertEquals
(
$scenarios
,
$model
->
scenarios
());
}
public
function
testIsAttributeRequired
()
...
...
@@ -234,3 +248,27 @@ class ModelTest extends TestCase
$invalid
->
createValidators
();
}
}
class
ComplexModel1
extends
Model
{
public
function
rules
()
{
return
[
[[
'id'
],
'required'
,
'except'
=>
'administration'
],
[[
'name'
,
'description'
],
'filter'
,
'filter'
=>
'trim'
],
[[
'is_disabled'
],
'boolean'
,
'on'
=>
'administration'
],
];
}
}
class
ComplexModel2
extends
Model
{
public
function
rules
()
{
return
[
[[
'id'
],
'required'
,
'except'
=>
'suddenlyUnexpectedScenario'
],
[[
'name'
,
'description'
],
'filter'
,
'filter'
=>
'trim'
],
[[
'is_disabled'
],
'boolean'
,
'on'
=>
'administration'
],
];
}
}
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