findAll X findAllByAttributes X IN

I have a model User with a relationship with another one Category.

And I want to retrieve all users of some categories.

I first tried:

$categories = array(1, 3, 5);

User::model()->with(‘categories’)->findAllByAttributes(array(‘categories.category_id’ => $categories));

Since findAllByAttributes accept arrays as parameters, and they are used as an In clause. But an exception was throwed because field categories.category_id doesn exist in user table.

Then I tried to use findAll:

User::model()->with(‘categories’)->findAll(‘categories.category_id = :categories’, array(’:categories’ => $categories));

But I got and error of array to string conversion.

So I there a way, using only ActiveRecords to do this (without convert previously the array of categories to a string)? findAllByAttributes accepts arrays parameters and put them in the IN clause, but not related activerecord, and findAll accepts related activerecords but not array arguments to put them in the IN clause of the SQL command.

Thank you.

How about IN condition?

Perfect! Thank you.