/** * @inheritdoc */ public function getAttributeValues() { $_att = []; $cas = CatalogAttributes::find()->where(['category_id' => $this->category_id])->orderBy('ordering')->all(); // foreach foreach ($cas as $ca) { $att = CatalogValues::find()->where(['catalog_id' => $this->id, 'attribute_id' => $ca->id])->one(); if (!$att) { $att = new CatalogValues(); $att->attribute_id = $ca->id; } $_att[] = $att; } //print_r($ca); die(); return $_att; // return $this->hasMany(CatalogValues::className(), ['catalog_id' => 'id'])->viaTable(CatalogAttributes::tableName(),['category_id' => 'category_id']); }
/** * @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(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CatalogAttributes::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'disabled' => $this->disabled]); $query->joinWith(['category' => function ($query) { $query->from(['category' => '{{%catalog_categories}}']); }]); $dataProvider->sort->attributes['category.title'] = ['asc' => ['category.title' => SORT_ASC], 'desc' => ['category.title' => SORT_DESC]]; $query->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'category.title', $this->getAttribute('category.title')]); return $dataProvider; }