For more details and usage information on IndexAction, see the guide article on rest controllers.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Action
 /**
  * Attach two-way sync needed behaviors for REST action
  * @param ActionEvent $event
  * @return bool
  * @throws NotSupportedException
  */
 public function beforeRestAction(ActionEvent $event)
 {
     /** @var Action $action */
     $action = $event->action;
     Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_INIT, [$this, 'makeModelSyncable'], null, false);
     switch (get_class($action)) {
         case IndexAction::className():
             $action->attachBehavior('indexLatest', IndexLatestBehavior::className());
             break;
         case CreateAction::className():
             // nothing to attach on create for now
             // TODO: check is needed to behave something here
             break;
         case UpdateAction::className():
             $action->attachBehavior('updateConflict', UpdateConflictBehavior::className());
             break;
         default:
             // TODO: implement custom new so called 'TwoWaySync action' stub for future custom actions
             // TODO: and add method attachSyncBehaviors()
             // $action->attachSyncBehaviors();
             //                throw new NotSupportedException("Not implemented yet");
             break;
     }
     return $event->isValid;
 }
Beispiel #2
0
 /**
  * @inheritdoc
  */
 protected function prepareDataProvider()
 {
     if (!isset($this->_dataProvider)) {
         $modelClass = $this->modelClass;
         $dummyModel = new $modelClass();
         $this->_dataProvider = $dataProvider = parent::prepareDataProvider();
         $this->_dataProvider->sort->attributes['descriptor'] = ['label' => 'Descriptor', 'asc' => $dummyModel->getDescriptorDefaultOrder($dummyModel->tableName(), SORT_ASC), 'desc' => $dummyModel->getDescriptorDefaultOrder($dummyModel->tableName(), SORT_DESC)];
         $this->_dataProvider->sort->defaultOrder = ['descriptor' => SORT_ASC];
         $objectType = $dummyModel->objectType;
         $query = false;
         if (!empty(Yii::$app->request->queryParams['query'])) {
             $query = $dataProvider->query->buildContainsQuery(Yii::$app->request->queryParams['query']);
         } elseif (!empty(Yii::$app->request->queryParams['advancedQuery'])) {
             $query = json_decode(Yii::$app->request->queryParams['advancedQuery'], true);
         }
         $whereConditions = $dataProvider->query->andWhereFromQuery($query);
         if (empty($objectType)) {
             throw new InvalidParamException($modelClass . ' does not have a corresponding object type');
         }
         if (!isset($this->parentObject)) {
             $dataProvider->query->denyInherit();
         } else {
             $registryClass = Yii::$app->classes['Registry'];
             $parentObject = $registryClass::get($this->parentObject, false);
             if (!$parentObject) {
                 throw new NotFoundHttpException("Object not found: {$this->parentObject}");
             }
             if (!$parentObject->can('read')) {
                 throw new ForbiddenHttpException("Unable to access {$this->parentObject}");
             }
             $newQuery = $parentObject->queryChildObjects($this->modelClass);
             $this->_dataProvider->query = $newQuery;
         }
     }
     return $this->_dataProvider;
 }
Beispiel #3
0
 protected function prepareDataProvider()
 {
     $dp = parent::prepareDataProvider();
     $urlParams = Yii::$app->getRequest()->queryParams;
     if (is_array($urlParams)) {
         if (isset($urlParams['criteria'])) {
             $criteria = Json::decode($urlParams['criteria']);
             $params = $this->getParams($criteria);
             if (isset($this->controller) && $this->controller->useSecureSearch) {
                 $conditionKeys = $this->getConditionKeys($criteria);
                 if (isset($conditionKeys) && count($conditionKeys) > 0) {
                     $c = $this->getConditionMapItem($conditionKeys, $params);
                     if (isset($c)) {
                         /** @var ActiveQuery $query */
                         if ($c instanceof ActiveQuery) {
                             $query = $c;
                         } else {
                             $query = Yii::createObject(ActiveQuery::className(), [$this->controller->modelClass]);
                             if (isset($c['condition']) || isset($c['with']) || isset($c['expand'])) {
                                 if (isset($c['condition'])) {
                                     $query->where($c['condition'], $params);
                                 }
                                 if (isset($c['with'])) {
                                     $with = $c['with'];
                                 }
                                 if (isset($c['expand'])) {
                                     $with = $c['expand'];
                                 }
                                 if (isset($with)) {
                                     $query->with($with);
                                 }
                             } else {
                                 $query->where($c, $params);
                             }
                         }
                     } else {
                         throw new BadRequestHttpException("Condition definition(s) not found: " . implode(',', $conditionKeys));
                     }
                 }
             } elseif (is_array($criteria) && isset($criteria['condition'])) {
                 /** @var ActiveQuery $query */
                 $query = Yii::createObject(ActiveQuery::className(), [$this->modelClass]);
                 $query->where($criteria['condition'], $params);
             }
             if (is_array($criteria)) {
                 if (isset($criteria['pagination']) && is_array($criteria['pagination'])) {
                     $pagination = $criteria['pagination'];
                     if (!isset($pagination['per-page']) && isset($pagination['perPage'])) {
                         $pagination['per-page'] = $pagination['perPage'];
                     }
                     $dp->setPagination(['params' => array_merge($urlParams, $pagination)]);
                 }
                 if (isset($criteria['sort'])) {
                     $dp->setSort(['params' => array_merge($urlParams, ['sort' => $criteria['sort']])]);
                     $sort = true;
                 }
             }
         }
         if (isset($query)) {
             $dp->query = $query;
         }
         if (isset($sort) || isset($urlParams['sort'])) {
             $dp->getSort()->enableMultiSort = true;
         }
     }
     return $dp;
 }