Browse Source

Added Model::loadMultiple() and validateMultiple().

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
16dcd9701f
  1. 28
      framework/yii/base/Controller.php
  2. 56
      framework/yii/base/Model.php

28
framework/yii/base/Controller.php

@ -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:

56
framework/yii/base/Model.php

@ -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

Loading…
Cancel
Save