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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
16dcd970
Commit
16dcd970
authored
Jun 26, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Model::loadMultiple() and validateMultiple().
parent
9cf82b3a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
30 deletions
+54
-30
Controller.php
framework/yii/base/Controller.php
+0
-28
Model.php
framework/yii/base/Model.php
+54
-2
No files found.
framework/yii/base/Controller.php
View file @
16dcd970
...
...
@@ -247,34 +247,6 @@ class Controller extends Component
}
/**
* Populates one or multiple models from the given data array.
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array.
* @param Model $model the model to be populated. If there are more than one model to be populated,
* you may supply them as additional parameters.
* @return boolean whether at least one model is successfully populated with the data.
*/
public
function
populate
(
$data
,
$model
)
{
$success
=
false
;
if
(
!
empty
(
$data
)
&&
is_array
(
$data
))
{
$models
=
func_get_args
();
array_shift
(
$models
);
foreach
(
$models
as
$model
)
{
/** @var Model $model */
$scope
=
$model
->
formName
();
if
(
$scope
==
''
)
{
$model
->
setAttributes
(
$data
);
$success
=
true
;
}
elseif
(
isset
(
$data
[
$scope
]))
{
$model
->
setAttributes
(
$data
[
$scope
]);
$success
=
true
;
}
}
}
return
$success
;
}
/**
* Renders a view and applies layout if available.
*
* The view to be rendered can be specified in one of the following formats:
...
...
framework/yii/base/Model.php
View file @
16dcd970
...
...
@@ -639,8 +639,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/**
* Populates the model with the data from end user.
* The data is subject to the safety check by [[setAttributes()]]. If [[formName()]] is not empty,
* the data indexed by [[formName()]] in `$data` will be used to populate the model.
* The data to be loaded is `$data[formName]`, where `formName` refers to the value of [[formName()]].
* If [[formName()]] is empty, the whole `$data` array will be used to populate the model.
* The data being populated is subject to the safety check by [[setAttributes()]].
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
* supplied by end user.
* @return boolean whether the model is successfully populated with some data.
...
...
@@ -660,6 +661,57 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
}
/**
* Populates a set of models with the data from end user.
* This method is mainly used to collect tabular data input.
* The data to be loaded for each model is `$data[formName][index]`, where `formName`
* refers to the value of [[formName()]], and `index` the index of the model in the `$models` array.
* If [[formName()]] is empty, `$data[index]` will be used to populate each model.
* The data being populated to each model is subject to the safety check by [[setAttributes()]].
* @param array $models the models to be populated. Note that all models should have the same class.
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
* supplied by end user.
* @return boolean whether the model is successfully populated with some data.
*/
public
static
function
loadMultiple
(
$models
,
$data
)
{
/** @var Model $model */
$model
=
reset
(
$models
);
if
(
$model
===
false
)
{
return
false
;
}
$success
=
false
;
$scope
=
$model
->
formName
();
foreach
(
$models
as
$i
=>
$model
)
{
if
(
$scope
==
''
)
{
if
(
isset
(
$data
[
$i
]))
{
$model
->
setAttributes
(
$data
[
$i
]);
$success
=
true
;
}
}
elseif
(
isset
(
$data
[
$scope
][
$i
]))
{
$model
->
setAttributes
(
$data
[
$scope
[
$i
]]);
$success
=
true
;
}
}
return
$success
;
}
/**
* Validates multiple models.
* @param array $models the models to be validated
* @return boolean whether all models are valid. False will be returned if one
* or multiple models have validation error.
*/
public
static
function
validateMultiple
(
$models
)
{
$valid
=
true
;
/** @var Model $model */
foreach
(
$models
as
$model
)
{
$valid
=
$model
->
validate
()
&&
$valid
;
}
return
$valid
;
}
/**
* Converts the object into an array.
* The default implementation will return [[attributes]].
* @return array the array representation of the object
...
...
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