public function search($params) { $tableName = $this->tableName(); $activeDataProvider = new ActiveDataProvider(['query' => static::find()]); if (!$this->load($params)) { return $activeDataProvider; } $query = $activeDataProvider->query; //Standart if ($columns = $this->getTableSchema()->columns) { /** * @var \yii\db\ColumnSchema $column */ foreach ($columns as $column) { if ($column->phpType == "integer") { $query->andFilterWhere([$this->tableName() . '.' . $column->name => $this->{$column->name}]); } else { if ($column->phpType == "string") { $query->andFilterWhere(['like', $this->tableName() . '.' . $column->name, $this->{$column->name}]); } } } } if ($this->section) { $query->joinWith('cmsContentElementTrees'); $query->andFilterWhere(['or', [$this->tableName() . '.tree_id' => $this->section], [CmsContentElementTree::tableName() . '.tree_id' => $this->section]]); } if ($this->created_at_from) { $query->andFilterWhere(['>=', $this->tableName() . '.created_at', \Yii::$app->formatter->asTimestamp(strtotime($this->created_at_from))]); } if ($this->created_at_to) { $query->andFilterWhere(['<=', $this->tableName() . '.created_at', \Yii::$app->formatter->asTimestamp(strtotime($this->created_at_to))]); } if ($this->updated_at_from) { $query->andFilterWhere(['>=', $this->tableName() . '.updated_at', \Yii::$app->formatter->asTimestamp(strtotime($this->updated_at_from))]); } if ($this->updated_at_to) { $query->andFilterWhere(['<=', $this->tableName() . '.created_at', \Yii::$app->formatter->asTimestamp(strtotime($this->updated_at_to))]); } if ($this->published_at_from) { $query->andFilterWhere(['>=', $this->tableName() . '.published_at', \Yii::$app->formatter->asTimestamp(strtotime($this->published_at_from))]); } if ($this->published_at_to) { $query->andFilterWhere(['<=', $this->tableName() . '.published_at', \Yii::$app->formatter->asTimestamp(strtotime($this->published_at_to))]); } if ($this->has_image) { $query->andFilterWhere(['>', $this->tableName() . '.image_id', 0]); } if ($this->has_full_image) { $query->andFilterWhere(['>', $this->tableName() . '.image_full_id', 0]); } if ($this->q) { $query->andFilterWhere(['or', ['like', $this->tableName() . '.name', $this->q], ['like', $this->tableName() . '.description_full', $this->q], ['like', $this->tableName() . '.description_short', $this->q]]); } return $activeDataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getCmsContentElementTrees() { return $this->hasMany(CmsContentElementTree::className(), ['element_id' => 'id']); }
protected function _run() { $cacheKey = $this->getCacheKey() . 'run'; $dependency = new TagDependency(['tags' => [$this->className() . (string) $this->namespace, (new CmsContentElement())->getTableCacheTag()]]); $result = \Yii::$app->cache->get($cacheKey); if ($result === false || $this->enabledRunCache == Cms::BOOL_N) { $this->initDataProvider(); if ($this->createdBy) { $this->dataProvider->query->andWhere([CmsContentElement::tableName() . '.created_by' => $this->createdBy]); } if ($this->active) { $this->dataProvider->query->andWhere([CmsContentElement::tableName() . '.active' => $this->active]); } if ($this->content_ids) { $this->dataProvider->query->andWhere([CmsContentElement::tableName() . '.content_id' => $this->content_ids]); } if ($this->limit) { $this->dataProvider->query->limit($this->limit); } $treeIds = (array) $this->tree_ids; if ($this->enabledCurrentTree == Cms::BOOL_Y) { $tree = \Yii::$app->cms->getCurrentTree(); if ($tree) { $treeIds[] = $tree->id; if ($tree->children && $this->enabledCurrentTreeChild == Cms::BOOL_Y) { if ($this->enabledCurrentTreeChildAll) { if ($childrens = $tree->children) { foreach ($childrens as $chidren) { $treeIds[] = $chidren->id; } } } else { if ($childrens = $tree->children) { foreach ($childrens as $chidren) { $treeIds[] = $chidren->id; } } } } } } if ($treeIds) { foreach ($treeIds as $key => $treeId) { if (!$treeId) { unset($treeIds[$key]); } } if ($treeIds) { /** * @var $query ActiveQuery */ $query = $this->dataProvider->query; $query->joinWith('cmsContentElementTrees'); $query->andWhere(['or', [CmsContentElement::tableName() . '.tree_id' => $treeIds], [CmsContentElementTree::tableName() . '.tree_id' => $treeIds]]); } } if ($this->enabledActiveTime == Cms::BOOL_Y) { $this->dataProvider->query->andWhere(["<=", CmsContentElement::tableName() . '.published_at', \Yii::$app->formatter->asTimestamp(time())]); $this->dataProvider->query->andWhere(['or', [">=", CmsContentElement::tableName() . '.published_to', \Yii::$app->formatter->asTimestamp(time())], [CmsContentElement::tableName() . '.published_to' => null]]); } /** * */ if ($this->with) { $this->dataProvider->query->with($this->with); } $this->dataProvider->query->groupBy([CmsContentElement::tableName() . '.id']); if ($this->activeQueryCallback && is_callable($this->activeQueryCallback)) { $callback = $this->activeQueryCallback; $callback($this->dataProvider->query); } if ($this->dataProviderCallback && is_callable($this->dataProviderCallback)) { $callback = $this->dataProviderCallback; $callback($this->dataProvider); } $result = parent::_run(); \Yii::$app->cache->set($cacheKey, $result, (int) $this->runCacheDuration, $dependency); } return $result; }