M
M
Mikha Pankratov2017-01-25 15:52:48
Yii
Mikha Pankratov, 2017-01-25 15:52:48

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

4 answer(s)
A
Alexey, 2017-01-25
@masterfreelance

SELECT * FROM user WHERE id not in (SELECT user_id FROM orders GROUP BY user_id)

D
Dmitry, 2017-01-25
@slo_nik

Good afternoon.
More or less like this:
Read more here

M
Maxim Timofeev, 2017-01-25
@webinar

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

D
Dmitry Bay, 2017-01-25
@kawabanga

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 question

Ask a Question

731 491 924 answers to any question