Answer the question
In order to leave comments, you need to log in
How to select all who did not pay?
Good afternoon,
there are 2 user/orders tables
to select all users who have paid, I use hasMany('orders', ['user_id' => 'id'])
and how to select all users who do not have records in the orders table?
Interested in ActiveRecord, link building
Answer the question
In order to leave comments, you need to log in
SELECT * FROM user WHERE id not in (SELECT user_id FROM orders GROUP BY user_id)
1 user has several orders, usually while orders can be paid and not paid, so yours
simply returns an array of user orders, which does not indicate payment in any way.
Usually, orders have a status_id field, which can be used to judge payment.
Therefore, the proposed option
will simply give all users who did not create an order.
Then, to select users with unpaid orders, the search should be on the order table that has a hasOne relationship with USER, for example:
$order = Order::find()->andWhere(['status_id'=>0])->all();
$users = ArrayHelper::getColumn($order,'user');
print_r($users);
Something like this, if you really need through communication. But from experience I’ll say that sometimes it’s easier to type through a separate request.
You can also set the matching array in the hasMany function itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question