コード例 #1
0
ファイル: Action.php プロジェクト: netis-pl/yii2-crud
 /**
  * @return ActiveSearchInterface|ActiveRecord
  */
 public function getSearchModel()
 {
     /** @var ActiveRecord $model */
     if ($this->controller instanceof ActiveController) {
         $model = $this->controller->getSearchModel();
     } else {
         $model = new $this->modelClass();
     }
     $params = Yii::$app->request->queryParams;
     $scope = $model->formName();
     if ($scope === '' && is_array($params) || $scope !== '' && isset($params[$scope]) && is_array($params[$scope])) {
         $model->load($params);
     }
     if (isset($params['ids'])) {
         $keys = Action::importKey($model::primaryKey(), Action::explodeKeys($params['ids']));
         $model->setAttributes($keys);
     }
     return $model;
 }
コード例 #2
0
 /**
  * Reads relations information from data sent by end user and uses it to link records to current model.
  * This is an equivalent to load() and save() methods.
  *
  * 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.
  * Keys should be relation names. Values are either:
  * - numeric arrays containing primary key values;
  * - associative array with keys: 'add', 'remove'.
  * Warning! Data will NOT be filtered or validated in any way.
  * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
  * supplied by end user.
  * @param string $formName the form name to be used for loading the data into the model.
  * If not set, [[formName()]] will be used.
  * @return bool whether no data was sent or the relations has been successfully linked.
  * @throws ForbiddenHttpException
  * @throws \yii\base\InvalidConfigException
  */
 public function saveRelations($data, $formName = null)
 {
     /** @var \yii\db\ActiveRecord $owner */
     $owner = $this->owner;
     $scope = $formName === null ? $owner->formName() : $formName;
     if ($scope !== '') {
         $data = isset($data[$scope]) ? $data[$scope] : [];
     }
     foreach ($data as $relationName => $keys) {
         if (($relation = $owner->getRelation($relationName, false)) === null) {
             continue;
         }
         if (is_string($keys) && trim($keys) === '') {
             continue;
         }
         if (!is_array($keys) || isset($keys[0])) {
             $keys = is_array($keys) ? $keys : Action::explodeKeys($keys);
             $addKeys = Action::importKey($owner::primaryKey(), $keys);
             $removeKeys = null;
         } elseif (is_array($keys) && isset($keys['add']) && isset($keys['remove'])) {
             $addKeys = Action::importKey($owner::primaryKey(), Action::explodeKeys($keys['add']));
             $removeKeys = Action::importKey($owner::primaryKey(), Action::explodeKeys($keys['remove']));
         } else {
             throw new InvalidCallException('Relation keys must be either a string, a numeric array or an array with \'add\' and \'remove\' keys.');
             continue;
         }
         $this->linkByKeys($relation, $addKeys, $removeKeys);
     }
     return true;
 }