Relational Filter and Pagination with HAS_MANY or MANY_MANY

This might be a frequently asked, repeated, and boring topic to some of you. But I’m a newbie on this one. ;)

As far as I understand so far, you can not use a relational filter and the the pagination at the same time when the relation is HAS_MANY or MANY_MANY.

For example, you have to construct the criteria using "with" and "together" if you want to filter with a relational column when you make a data provider for a grid.

$criteria->with = array('foo');

$criteria->together = true;


$criteria->compare('', $this->bar, true);


This works fine as long as the filtering concerns.

But you will know that a page in the grid may contain less number of rows than expected. For instance, a page that should have 20 rows may contain only 10 or so, it depends on the concrete data. :(

If you want the pagination to work as expected, you have to set “together” to false or null, and you have to give up the relational filter. (When you omit the “together” line in the above, the “compare” line will give an error, saying “I don’t know a column named”.) :(

Am I correct so far? If I have overlooked something, please correct me.

And I don’t call it a bug. I think I know what is the reason of this limitation.

So, what I’d like to ask all of you is, how do you manage to get together the relational filter and the pagination when the relation is HAS_MANY or MANY_MANY? I’m thinking about using CArrayDataProvider, but I’m afraid that it may be expensive for a large set of data. What do you think? Is there an easy way round?

In this case i use:

public function getMyRelation()


$searcher=new MyRelationModel();

return $searcher->searchBy<OwnerNameModel>($params);


where searchBy<OwnerNameModel> is:

return new CActiveDataProvider(

//... criteria and others





Thank you for your response, Ekstazi,

But could you explain your code a bit more in details? I don’t think I got the point.

Your searchBy<OwnerNameModel>() method returns a CActiveDataProvider of <RelationModel>. And where do I use it when I want to create a CActiveDataProvider of <OwnerNameModel> for a grid view?

The same issue:

Issue 2678

Bump, just for once.

I would appreciate any idea.