I’ve opened the same question on SO here.
Part of my schema is a pictured:
The junction table maps which items are in which category, with the "featured" column marking the item as featured in that category.
What I need to do is get a list of all items in a specific category, including the "featured" attribute from the junction table and pass that data for display into a gridview.
I have created a model for the category_item table and tried to implement the solution proposed here but how do I access and pass the data into my gridview?
Category Model
public function getCategoryItems()
{
return $this->hasMany(CategoryItem::className(), ['category_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])->viaTable('{{%category_item}}', ['category_id' => 'id']);
}
CategorySearch Model
Copied line 3 from above link
public function search($params)
{
$query = Category::find()->with('items')->with('categoryItems');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'type' => $this->type,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
Item Model
public function getItemCategories()
{
return $this->hasMany(CategoryItem::className(), ['item_id' => 'id']);
}
public function getCategories()
{
return $this->hasMany(Category::className(), ['id' => 'category_id'])->viaTable('{{%category_item}}', ['item_id' => 'id']);
}
All ideas welcome!