Answer the question
In order to leave comments, you need to log in
How to make a selection using hasMany?
Hello. I can't figure out how to do it.
In general, I have a product_id and I need to get a shop_id, so I sketched an example from the database,
i.e. go in order from left to right through the tables in order, choosing the necessary fields, I try to do this, in the product table model I call getshopCity ()
public function getProductPrice()
{
return $this->hasMany(shopProductPrice::className(),['product_id'=>'product_id']);
}
public function getProductCity()
{
return $this->hasMany(shopCity::className(),['shop_city_id'=>'shop_city_id'])->via('productPrice');
}
public function getshopCity()
{
return $this->hasMany(shop::className(),['shop_id'=>'shop_id'])->via('productCity');
}
return $this->hasMany(shopProductPrice::className(),['shop_Product_id'=>'shop_Product_id']);
Answer the question
In order to leave comments, you need to log in
Firstly: You unsuccessfully chose the names of the tables and fields in them (and, accordingly, the models)
1. shop_city - in the comment you write that this is a list of branches, and judging by the name, just a link table. It's better to replace its name with something more suitable, for example (the first thing that came to mind): shop_office
2. shop_product_price - by the name you can say that these are the prices of goods in the store, but if you delve into it, it turns out that these are the prices of goods in the STORE BRANCHES .
secondly: the link that selects the store should not be in the Product model, but in the branch model, and the names of the links themselves are not chosen well - they only confuse.
Those. in the Goods model there must be a link that selects branches, in the branch model - a link that selects a store
or if you need to display products that have all links
Product::find()->joinWith(['<название связи с филиалом>.<название связи филиала с магазином>'], true, 'RIGHT JOIN')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question