我得到了MySQL查询,我怎么用ActiveRecord写这个?
SELECT *
FROM `sel_posts`
WHERE `login_id` = 22 OR 23 OR 24 ... (this ids from array $ifollow)
ORDER BY `datetime` DESC
LIMIT 0 , 20发布于 2015-03-29 01:34:04
首先,最好在WHERE部件中用IN替换OR。
假设模型名为SelPost,下面是使用ActiveQuery执行此操作的方法
$models = SelPost::find()
->where(['login_id' => $ifollow])
->orderBy(['datetime' => SORT_DESC])
->limit(20)
->all();where足够智能,可以在$ifollow为数组的情况下自动添加in运算符。
官方文档:
https://stackoverflow.com/questions/29319455
复制相似问题