Baffled by search and criteria for CGridView

Hey everyone,

I have two tables, Samples and Species. Samples has a Foreign Key, "SpeciesID", referring to the Species table having SpeciesID as primary key

I managed to use the relations() function in the SamplesController so that the CGridView in the Admin of the Samples shows the SciName of the Species instead of the SpeciesID. I think relations() is OK.

I am struggling with making it searchable. This kind of question has been asked many times, I know, but somehow I can’t grasp the general idea.

It is probably the search() that I am not doing properly.

My code:

//In Samples

public function rules()


return array



array('SampleID, SciName, SensorID, ModelNumber, DateTime, Latitude, Longitude, Altitude, SampleType, SampleCondition, Comment, PhotoFilename, SpectrumFile', 'safe', 'on'=>'search'),



	public function relations()


		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.


		return array('GetSciName'=>array(self::BELONGS_TO, 'Species', 'SpeciesID'),






	public function search()


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

		// should not be searched.

		$criteria=new CDbCriteria;



		$criteria->with = 'GetSciName';

		$criteria->compare('GetSciName.SciName', $this->SciName, true);//somehow doesnt work?

//The CGridView seems OK, it shows the Species name "SciName" not the SpeciesID... just not searchable (yet)

<?php $this->widget('zii.widgets.grid.CGridView', array(




	'emptyText'=>'Not available',





		array(	'name'=>'SpeciesID',


















)); ?>

have you checkout the comments at


I have the same problem, and i have read all the posts, but still, cant make it work.

Whatever the solution is, should be made a tutorial… everyone and their mothers are using tables with foreign keys! :)

Err… I found my problem…

At an earlier stage in my work I saw in SamplesController::actionAdmin the CRUD code generated by Gii declares

$model=new Samples('search');

I was puzzled that the Samples constructor took an argument “search”, it doesn’t happen in other places. The API document says nothing about taking argument? I changed it to $model=new Samples and even $model=Samples::model() without affecting anything. At that time I wasn’t doing the Foreign Key search yet. The normal searches worked just fine.

I forgot to change it back. When I tried to do the Foreign Key Search, it didn’t work. It works now when I changed it back to Samples(‘search’).

In some way I don’t understand, giving Samples the argument “search” makes the foreign key search work… this is puzzling… I’ll just have to to take it as a dogma. I will never question the wisdom of Gii.

And this thread has been most useful to me!

Pity it doesn’t turn up on the first page of the forum search. :)

One more thing… as that thread mentions, the sorting function by clicking the column header is disabled if you do this kind of foreign key search, any help there?

I’m still having problems with cgrid, its nice to know that you don’t xD!. Anyways, here’s what i’m doing:

in the model

class Telefonos extends CActiveRecord


	public $nombre_usuario;


public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('usr_id, ttel_id, num_telefono', 'required'),

			array('usr_id, ttel_id', 'numerical', 'integerOnly'=>true),

			array('num_telefono', 'length', 'max'=>11),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('tel_id, usr_id, ttel_id, num_telefono, nombre_usuario,', 'safe', 'on'=>'search'),




public function search()


		$criteria= new CDbCriteria;









		return new CActiveDataProvider(get_class($this), array(




And in the view

<?php $this->widget('zii.widgets.grid.CGridView', array(











)); ?>

Help anyone?

By the way, for the sorting, here’s a link that might help you:

Ok, my problem is solved. i was missing this in the controller: