Model Relations using GridView

Hi,

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

http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/

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 = city.id LEFT JOIN country ON tour.country_id = country.id

SELECT SELECT * FROM country WHERE id=1

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.

Thanks!

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:

http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/

Or you can read my post here:

http://www.yiiframework.com/forum/index.php/topic/63017-related-search-and-attributelabels/page__p__278193

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

Hope this helps!

Regards

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