Sorting by relationship value

Hello, I have a list of items in the admin (manage) page for my Stories model. Each Story has an Engine associated with it. The first column in this admin view is “Engine Name”, which is part of the Engine table. This relationship is defined in the Story and Engine model files. However, on this page I can’t search by Engine Name. The input box for filtering items is gone for the Engine Name, but still there for the other columns. Also, I’m not sure how to allow for searching by Engine Name in the advanced view either. Any help is appreciated, thanks!

Add it to search. :)

Like this:

	public function search() {

    	// Warning: Please modify the following code to remove attributes that

    	// should not be searched.

    	$criteria = new CDbCriteria;

    	$criteria->with = array('engine'); // use name of your relation here

    	$criteria->compare('id', $this->id);

    	$criteria->compare('', $this->engine_id);


And then, in the view:


                	'name' => 'engine_id',

                	'header' => 'Engine Name',

                	'value' => '(($data->version)?$data->engine->name:"")',

                	'filter' => $this->getEngineFilter(),



'getEngineFilter retuns an array of all engine names.

ugh nothing’s working, I’ll have to give it a try later. Thanks.

Make sure that you’re using an array like:

foreach($engines as $engine) {

$array[$engine->name] = $engine->name;


return $array;

Then you should get a nice filter dropdown in the grid.

Thanks it’s working great!