I have a hasMany relation declared in one of my models and I want to filter the result of my query.
Imagine I have a cart entity with CartModel declared and a order entity with OrderModel declared. I have declared a relation named orders (getOrders) in which we fetch orders of a cart. I want to fetch carts and it’s orders with the following condition. Order status should be ‘active’. My query looks like the following:
$carts = \app\models\Cart::find()->joinWith(‘orders’)->where([‘order.status’ => ‘active’])->all();
This should change to:
SELECT * FROM cart LEFT JOIN order ON order.cart_id=cart.cart_id WHERE order.status = ‘active’
However my ActiveRecord query fetches carts and all orders regardless of my condition.
What should I do to filter joinWith results?
P.S. I cannot declare onCondition since the condition part is generated dynamically.