23

Is it possible to make WHERE IN condition with active record? I am going through documentation, but I can't find this case. I need to perform this query and return count value.

This is the method I am using for now:

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

1 Answer 1

48

As documentation about where() says:

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

so your method should be correct. If you want to use IN then your code should look like this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.