Yii2 - WHERE IN有活动记录

时间:2015-07-07 14:29:45

标签: yii2

是否可以使用活动记录制作WHERE IN条件?我正在阅读文档,但我找不到这种情况。我需要执行此查询并返回计数值。

这是我现在使用的方法:

public function static findProductsOnSale($ids)
    {
        return $this->find()->where(['product_id' => $ids])->count();
    }

1 个答案:

答案 0 :(得分:22)

正如documentation about where()所说:

  

['id'=> [1,2,3],'status'=> 2]生成(id IN(1,2,3))AND(status = 2)

所以你的方法应该是正确的。 如果您想使用 IN ,那么您的代码应如下所示:

public function static findProductsOnSale($ids)
{
    return $this->find()->where(['in', 'product_id', $ids])->count();
}
相关问题