public function findModel($id) { if ($this->findModel !== null) { return call_user_func($this->findModel, $id, $this); } /* @var $modelClass ActiveRecordInterface */ $modelClass = $this->modelClass; $keys = $modelClass::primaryKey(); $model = null; if (count($keys) > 1) { $values = explode(',', $id); if (count($keys) === count($values)) { $model = $modelClass::find()->where(array_combine($keys, $values)); } } elseif ($id !== null) { $model = $modelClass::find()->where(array_combine($keys, [$id])); } if (isset($model)) { if ($with = ActionHelpers::getWith()) { $model->with($with); } return $model->asArray()->one(); } else { throw new NotFoundHttpException("Object not found: {$id}"); } }
/** * Prepares the data provider that should return the requested collection of the models. * @return ActiveDataProvider */ protected function prepareDataProvider() { if ($this->prepareDataProvider !== null) { return call_user_func($this->prepareDataProvider, $this); } /* @var $modelClass \yii\db\BaseActiveRecord */ $modelClass = $this->modelClass; $query = $modelClass::find(); $filters = ActionHelpers::getFilter(); foreach ($filters as $filter) { $query->andFilterWhere($filter); } if ($with = ActionHelpers::getWith()) { $query->with($with); } return new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => ActionHelpers::getLimit()], 'sort' => ActionHelpers::getSort()]); }