P
P
Pavel Shorokhov2015-10-25 13:50:45
Yii
Pavel Shorokhov, 2015-10-25 13:50:45

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();
}

Similarly in products, and everything works.
Problem
But we want to add information that such and such a customer has such and such a product in such and such quantity. And add the count column to the customer_has_product table.
Actually the question is how to get such data as count, which are in the link table?
What are the standard solutions to this problem and how would you implement it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question