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->compare('SampleID',$this->SampleID);

	


		$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(

	'id'=>'samples-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'emptyText'=>'Not available',

	'columns'=>array(

		'SampleID',

		

		//'SpeciesID',

		array(	'name'=>'SpeciesID',

			'value'=>'$data->GetSciName->SciName'),

		


		'DateTime',

		'Latitude',

		'Longitude',

		

		'Altitude',

		'SampleType',

		'SampleCondition',

		'Comment',


		'PhotoFilename',

		'SpectrumFile',

		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>






have you checkout the comments at

CActiveRecord

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!

http://www.yiiframework.com/forum/index.php?/topic/11546-cgridview-with-mulitple-model/page__p__66736__hl__admin+earch+foreign+key#entry66736

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;

		

		$criteria->compare('ttel_id',3);

		//$criteria->compare('usr.usr_nombre',$this->nomUsr,true);

		$criteria->compare('usr.usr_estatus',1);

		

		$criteria->with=array('usr');

		if(strlen($this->nombre_usuario))

			$criteria->addSearchCondition('usr.usr_nombre',$this->nombre_usuario,true);

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

			'criteria'=>$criteria,

		));

	}



And in the view




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

	'id'=>'telefonos-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		array('name'=>'nombre_usuario','value'=>'$data->usr->usr_nombre','header'=>'Nombre'),

		'num_telefono',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Help anyone?

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

http://www.yiiframework.com/forum/index.php?/topic/8784-cgridview-sort-columns/

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




if(isset($_GET['Telefonos']))

		$model->attributes=$_GET['Telefonos'];