public function search($q) { $sub = explode(' ', $q); /* get from catalog */ $rows_query = (new \yii\db\Query())->select(["CONCAT('catalog') AS module", 'id' => 'id', 'text' => 'title'])->from(['t' => \app\modules\catalog\models\Catalog::tableName()]); foreach ($sub as $word) { $rows_query->andWhere(['or', ['like', 'title', $word], ['like', 'content', $word], ['exists', (new \yii\db\Query())->select(['id'])->from(['v' => \app\modules\catalog\models\CatalogValues::tableName()])->where('v.catalog_id = t.id')->andWhere(['like', 'value', $word])]]); } foreach ($rows_query->all() as $row) { $this->_results[] = ['url' => Url::to(['/catalog/position/view', 'id' => $row['id']]), 'text' => $row['text']]; } return $this->_results; }
/** * @inheritdoc */ public function init() { if (!$this->category_id) { throw new InvalidConfigException('The "category_id" property must be set.'); } $query = Catalog::find()->select(['id', 'title', 'cost', 'category_id'])->where(['category_id' => $this->category_id]); $this->categoryAttributes = CatalogAttributes::find()->where(['category_id' => $this->category_id])->orderBy('id')->all(); $query = (new \yii\db\Query())->select(['t.id', 't.title', 't.cost', 't.category_id'])->from(['t' => $query]); foreach ($this->categoryAttributes as $x) { $query->addSelect([$x->code . '.value as ' . $x->code]); $query->leftJoin([$x->code => (new \yii\db\Query())->from(CatalogValues::tableName())], $x->code . '.catalog_id = t.id and ' . $x->code . '.attribute_id = ' . $x->id); $query->addSelect(['categoryAttribute[' . $x->id . ']' => 'categoryAttribute[' . $x->id . '].value']); $query->leftJoin(['categoryAttribute[' . $x->id . ']' => (new \yii\db\Query())->from(CatalogValues::tableName())], ['categoryAttribute[' . $x->id . '].catalog_id' => 't.id', 'categoryAttribute[' . $x->id . '].attribute_id' => $x->id]); } $query = (new \yii\db\Query())->select(['t.*'])->from(['t' => $query]); $this->query = $query; parent::init(); }