Table Search

Hello,

I’ve an admin manage table with search input. The input in the table is stored as an ID, but it displays the corresponding text.

Example:




ID | DOMAIN     |  HOST    |

---+------------+-----------

[ ]| [        ] | [       ]

---+------------+----------- 

12 | google.com | yahoo.com (stored as 3)

---+------------+----------- 



Yahoo is stored in the database as a integer, but I don’t want to search on integers, but on text. So if I search for yah, I get all hosts that contain yah in it!

Can someone help me?

Regards,

Vincent

Please show your code that prepares criteria for CActiveDataProvider.

Oh sorry about that! Here is the part of the code where the table is displayed, I deleted some parts to make it more organized.




<?php

/* @var $this DomainController */

/* @var $model Domain */


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

	'id'=>'domain-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		'url',

                array(

                     'name' => 'primary_url_id',

                     'header' => 'Primary URL',

                     'value' => '$data->domain->url',

                ), 

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



I think the answer is in this part of code, this is the Domain model:




	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


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

		$criteria->compare('url',$this->url,true);

		$criteria->compare('primary_url_id',$this->primary_url_id, true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



Change:




$criteria->compare('primary_url_id',$this->primary_url_id, true);



Into:




$criteria->compare('domain.url',$this->primary_url_id, true);

// 'together' will force a left join, that's extra cost for searching in relations

$criteria->with = array('domain'=>array('together'=>true)); 



Just make sure that the ‘primary_url_id’ attribute is not limited to integers in the ‘search’ scenario. I know this looks strange but is it’s valid anyway.

Thanks! It works now!

Please post question in General Discussion for Yii 1.1.x rather than Yii-powered Apps