Pagination With Many_Many

Hi,

I have a model Unit

and I have a relation in Unit model defined as


'endUsers' => array(self::MANY_MANY, 'Enduser', 'enduser__unit(unit_id, enduser_id)'),

Now when I do a


$criteria->with = array('endUsers'=>array('select'=>'endUsers.name','together'=>true));

I get something like Displaying 1-5 of 19 results even though the pageSize is set to 6. This could be because some Unit has 2 Endusers.

How do I get through this?

Thanks,

S

Hi,

I think u have some duplicate records… just check your database

Thanks for the response.

There are no duplicates records. If I remove that criteria, I get expected results. I believe its because some Unit has 2 Endusers. How do I handle the pagination for this?

By setting ‘together’ to false, you will get the correct number of Unit models. But by doing so, you will not be able to search Units by the related endUsers anymore. It’s a trade off.

For detailed information about this problem, please read the following wikis.

http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/

http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/

I was wondering the same. Thanks for clearing this up.

But if I set ‘together’=> false , I dont see how I can atleast compare. It shows a ‘column not found’ DB error , where in the error I dont see this Table’s join.

Please take a closer look at the second wiki I mentioned.

My bad.

Many thanks for the links. I am going through those and will come back if I have any query.

Never mind.

In fact, the problem had been a very tough one for me. I started writing the "Drills" wiki just to illustrate the dilemma: you could just choose the one or the other according to the circumstance. I had been thinking that it should be a problem without any clear answer.

I think I could provide a decent solution to the problem in the wiki at least for MySQL users.