yii\rest\IndexAction::prepareDataProvider PHP Method

prepareDataProvider() protected method

Prepares the data provider that should return the requested collection of the models.
protected prepareDataProvider ( ) : ActiveDataProvider
return yii\data\ActiveDataProvider
    protected function prepareDataProvider()
    {
        if ($this->prepareDataProvider !== null) {
            return call_user_func($this->prepareDataProvider, $this);
        }
        /* @var $modelClass \yii\db\BaseActiveRecord */
        $modelClass = $this->modelClass;
        return new ActiveDataProvider(['query' => $modelClass::find()]);
    }

Usage Example

Beispiel #1
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;
 }
All Usage Examples Of yii\rest\IndexAction::prepareDataProvider