Yii Framework Forum: CGridView Search in a relational field - Yii Framework Forum

Jump to content

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

CGridView Search in a relational field Rate Topic: -----

#1 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 05 May 2010 - 03:45 PM

I have a display grid with orders. All orders have a username attached so it makes a lot of sense to search within a relational field (searching orders by username). Problem is, I can't get the grid to perform a search on relational fields. Here's my code..:

public function relations () {
	return array ( 
		
		'user' => array ( 
			
			self::BELONGS_TO, 
			'User', 
			'user_id' 
		) 
	);
}

public function search () {
	$criteria = new CDbCriteria ();
	$criteria->order = 'date_added DESC';
	$criteria->compare ( 'transaction_id', $this->transaction_id, true );
	$criteria->compare ( 'status', $this->status, true );
        
        //        'troo' is just part of an existing username I have
	$criteria->compare ( 'user.username', 'troo', true );
	
	return new CActiveDataProvider ( 'Order', array ( 
		
		'criteria' => $criteria 
	) );
}


Running this code throws an error like: CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.username' in 'where clause'.

So how does one search through a relational field?
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 06 May 2010 - 01:47 AM

Try to add:

$criteria->with='user';

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