/** * Finds the Item model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer|boolean $id * @param boolean $search * @return Item|ItemSearch the loaded model * @throws NotFoundHttpException if the model cannot be found */ public function findModel($id = false, $search = false) { $model = Module::getModel($this, $search ? $this->searchModelName : $this->modelName, ['controllers' => 'models'], ['scenario' => $this->getScenario()]); if ($id && !($model = $model->search(compact('id'))->one())) { throw new NotFoundHttpException('The requested page does not exist.'); } $model->scenario = $this->getScenario(); // for found model too return $model; }
protected function generateItems() { $moduleName = Module::moduleName($this); $model = Module::getModel($this, 'Category', ['widgets' => 'models']); $items = $model::find()->orderBy(['lft' => SORT_ASC])->asArray()->all(); foreach ($items as &$item) { $uniqueKey = self::$uniqueKey++; $nodeId = $uniqueKey . '-id-' . $item['id']; $item['nodeAttributes'] = ['id' => $nodeId, 'text' => $item['name'], 'type' => 'folder', 'active' => \Yii::$app->request->get('id') == $item['id'], 'a_attr' => ['data-id' => $nodeId, 'href' => ["/{$moduleName}/category/update", 'id' => $item['id']]]]; } return $items; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params = []) { $model = Module::getModel($this, 'Category', ['scenario' => $this->scenario]); /** * @var ActiveQuery $query */ $query = $model->search(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->andFilterWhere(['is_active' => $this->is_active]); return $dataProvider; } $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['is_active' => $this->is_active])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params = []) { $model = Module::getModel($this, 'Item', [], ['scenario' => $this->scenario]); /** * @var ActiveQuery $query */ $query = $model->search(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'is_active' => $this->is_active]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'brief', $this->brief])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'DATE_FORMAT(FROM_UNIXTIME(created_at), "%Y-%m-%d")', $this->created_at]); if ($this->category_id) { $t = $this->tableName(); $relation = \bariew\postModule\Module::getModel($this, 'CategoryToItem')->tableName(); $query->innerJoin($relation, "{$relation}.item_id = {$t}.id")->andWhere(["{$relation}.category_id" => $this->category_id]); } return $dataProvider; }
/** * * @param string $formName * @return ActiveRecord $model */ protected function getModel($formName) { return Module::getModel($this->owner, $formName); }
/** * @return ActiveQuery */ public function getItems() { $relation = Module::getModel($this, 'Item'); return self::hasMany($relation::className(), ['id' => 'item_id'])->via('categoryToItems'); }