Yii Framework Forum: [SOLVED] How do I search on a join in admin.php - Yii Framework Forum

Jump to content

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

[SOLVED] How do I search on a join in admin.php Rate Topic: -----

#1 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 10:13 AM

Hello,

I added a joined field cat.cat_name to the cgridview and cannot search on it.
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'pd_id',
		'cat.cat_name',

Regards,

Frank
0

#2 User is offline   derelict 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 18-November 09
  • Location:Ukraine

Posted 19 November 2010 - 10:20 AM

You need to configure sort property of CActiveDataProvider in search method of your model.
UPD: I read not inattentively and think you need sort not search. :(
0

#3 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 10:35 AM

Do you have an example?
I am still new to yii.

Thanks
Regards,

Frank
0

#4 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 19 November 2010 - 11:19 AM

You need to update the search method of the model to include the join:

public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->join = 'LEFT JOIN related_tbl as tbl_alias ON tbl_alias.id =  fk_id';

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

		//... standard criteria here
		
		$criteria->compare(' tbl_alias.searchable1',$this->tbl_alias->searchable1, true);
		
		// etc. add the other fields of the relation tbl_alias that you want to access
		
		return new CActiveDataProvider('MyModelName', array(
			'criteria'=>$criteria,
		));
	}


This works well for me. There may be a more elegant solution to the join.
0

#5 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 11:38 AM

Thanks,

I need to do another join, even through I have it joined in relations?
Regards,

Frank
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 November 2010 - 11:45 AM

Nope.

In your models 'search' function you probably have a list of criteria compares, like this:
        $criteria->compare('id', $this->id);


If you want to search by a related models field, then include that in the list of criteria:
        $criteria->compare('cat_name', $this->cat_id);


To make that work, put this before the compare conditions:
        $criteria->with = array('cat');


'with' is just an elegant way of declaring a join.
You need to include all related tables, of course.
And you might need to prefix some of the field names due to ambuigity.
"Less noise - more signal"
0

#7 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 11:57 AM

Still not working. Here is my code.
public $search_location;

public function relations()
  'cat' => array(self::BELONGS_TO, 'Category', 'cat_id'),

$criteria->with = array('cat');
$criteria->compare('cat_name', $this->search_location);

$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'pd_id',
		 array(            
                  'name'=>'search_location',
                  'value'=>'$data->cat->cat_name',
                  'header'=>'Category',
                ),

Regards,

Frank
0

#8 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 12:00 PM

Thank you all, It is working now.
I had to add the true word.

I used jacmoe solution.

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

Regards,

Frank
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 November 2010 - 12:04 PM

<edit> I saw that you fixed it - great! :) </edit>
"Less noise - more signal"
0

#10 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 19 November 2010 - 12:09 PM

I really appreciate the help.
I am coming from a codeigniter background.
Regards,

Frank
0

#11 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 20 November 2010 - 08:06 AM

View Postfrocco, on 19 November 2010 - 12:09 PM, said:

I really appreciate the help.
I am coming from a codeigniter background.

I'm coming from CI too, your among friends here!

doodle
Check out myYii powered website
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