Answer the question
In order to leave comments, you need to log in
Condition for hasMany with linked table?
Good afternoon, there are 2 tables:
1. Product (id, name, hasDelivery)
2. Delivery (id, product_id, text)
I need to get all products + delivery (only for products with hasDelivery = 1), how to implement this?
Now I have
public function getDelivery(){
return $this->hasMany(Delivery::className(), ['product_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
If there is no delivery, then delivery will also contain an empty array, I would not even create the hasDelivery column, since it is redundant, by the presence of related rows, it is clear whether hasDelivery or not. So I don't see the problem, but you can try this:
public function getDelivery(){
return $this->hasDelivery ? $this->hasMany(Delivery::className(), ['product_id' => 'id']) : null;
}
hasDelivery
boolean, etc. public function getDelivery(){
return $this->hasMany(Delivery::className(), ['product_id' => 'id']);
}
public function hasDelivery(){
return count($this->delivery > 0);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question