Yii Framework Forum: Table Search - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Table Search Rate Topic: -----

#1 User is offline   vvdboogaard 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-September 13

Posted 12 September 2013 - 07:08 AM

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
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 12 September 2013 - 08:12 AM

Please show your code that prepares criteria for CActiveDataProvider.
Don't be a dick.
0

#3 User is offline   vvdboogaard 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-September 13

Posted 12 September 2013 - 08:54 AM

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,
		));
	}

0

#4 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 12 September 2013 - 09:11 AM

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.
Don't be a dick.
0

#5 User is offline   vvdboogaard 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 12-September 13

Posted 12 September 2013 - 09:34 AM

Thanks! It works now!
0

#6 User is offline   kiran sharma 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 493
  • Joined: 21-May 11
  • Location:India

Posted 12 September 2013 - 11:46 PM

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

View Postvvdboogaard, on 12 September 2013 - 07:08 AM, said:

Hello,
..

Thanks,
Kiran Sharma.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users