Model Relations using GridView


I want to be able to search and sort in gridView using fields from related tables. I found these instructions:

I was able to use that information to create a working test program. When I use the debugger to view the underlying SQL, it appears that the same tables are being queried twice: once through the join to do the filtering and sorting and a second time through the relationship to display the city and country names:

[sql]SELECT SELECT tour.* FROM tour LEFT JOIN city ON tour.city_id = LEFT JOIN country ON tour.country_id =


SELECT SELECT * FROM city WHERE id=1[/sql]

Is there a way in gridView to request the city and country name columns from the first query that joins all the tables together, instead of using the model relationship which loads that information from subsequent queries? This would prevent the addition queries against the same tables.


use lazy loading. Look near the bottom of the guide for the relations portion and lazy loading.

Guide on working with data

This helped me to get related search working:

Or you can read my post here:

at the beginning of the post (inside the spoiler) I basically posted everything needed to do related search.

Hope this helps!


I think I’m actually trying to avoid lazy loading. I believe lazy loading is adding the extra queries.