Answer the question
In order to leave comments, you need to log in
Yii2 how to get data from link table when working with many-many?
Example
Standard many-many example, customers and products, one customer can buy many products, one product can be bought by different customers. Accordingly, we have 3 tables:
customer, product - models
customer_has_product - a relationship table with two columns customer_id, product_id
In Yii2, we set up in the Customer model:
public function getProducts()
{
return $this->hasMany(Product::className(), ['id' => 'product_id'])
->viaTable('customer_has_product', ['customer_id' => 'id'])
->all();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question