S
S
Sergey2015-03-28 18:53:15
Yii
Sergey, 2015-03-28 18:53:15

How to pull data from the database, according to the IDs in the array, using activeRecord in yii2?

Something I'm stupid, gentlemen, I got lost in three pines, as they say.
There is a table with posts. I need to get out of it the posts of the people I follow.
The IDs of these people are in the array, for example $ifollow.
Posts should be sorted by date and taken in 20 pieces by Ajax.
Those. the request is like this:

SELECT *
FROM `sel_posts`
WHERE `login_id` = 22 OR 23 OR 24 ... (все значения массива по сути)
ORDER BY `datetime` DESC
LIMIT 0 , 20

But how to do it correctly, through yii2 objects, using ActiveRecord, and even sort out another array in the login_id values ​​- I find it difficult. For a long time, I would have simply made queries through mysql, but you have to do it right, according to feng shui.
Can you tell me how to spin it?
How to do it right and according to Feng Shui?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2015-03-28
@butteff

$ids = [1, 2, 3, 4, 5];
$items = SelPost::find()->where(['login_id' => $ids])->order('datetime desc')->limit(20)->all();

S
Sergey, 2015-03-29
@alekskondr

Hi all!
Tell me how to implement it? When entering data in one field, this data should be added to two tables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question