More details (such as how you construct your dataprovider, and your database structure) might help.
I assume that you have the normal database structure with a location table, and a sublocation table that contains a foreign key referencing the location table. I also assume, given the parameters you are using, that your $model is created from the LocationSub model, so all those extra queries you are seeing are a result of listing sublocations, then lazy loading the appropriate location for each one in order to output the name.
In this case, as far as I know there is no standard way to use eager loading instead, since you are using the model search method in order to filter, but it should be fairly easy to modify the model search method either to hard-code eager loading for certain attributes when searching, or to add an optional CDbCriteria parameter to be passed to the search method, which is then merged with the search parameters. You could also of course modify the relationship in the LocationSub model to add a ‘with’ criteria to the relationship, but this would mean that the parent Location would be appended to LocationSub in whatever context it is used in, even if not needed.
I would suggest you modify your search method, which probably looks a bit like this:
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('loc_no',$this->loc_no,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
to something like this:
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search($extraCriteria = new CDbCriteria)
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('loc_no',$this->loc_no,true);
$criteria->mergeWith($extraCriteria);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
This will allow you to pass extra criteria to your search, so in this case, in order to eager load the parent Locations rather than lazy load, you would change your dataProvider for the CGridView to
‘dataProvider’ => $model->search(new CDbCriteria(array(‘with’ => ‘fkLoc’))),
Hopefully this should help you out…