M
M
Myma2021-08-18 05:49:22
MySQL
Myma, 2021-08-18 05:49:22

How to organize a link in Yii2 through a table with conditions?

In general, the query itself looks like this
SELECT * FROM `vk_users` WHERE `user_id` = 24 AND `chat_bot_clients_id` in (SELECT `id` from `chat_bot_clients` where `chat_bot_user_id` = 1)

user_id is the ID of the model that hasMany is associated with vk_users
chat_bot_user_id is the ID of the current user

How to write a communication function correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2021-08-18
@Myma

It seems that this approach should work if I correctly understood what you meant by ActiveRecord:

$subQuery = chat_bot_clients::find()
    ->select('id')
    ->where(['chat_bot_user_id' => 1]);

$query = vk_users::find()
    ->where(['user_id' => 24])
    ->andWhere(['in', 'chat_bot_clients_id', $subQuery]);

$models = $query->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question