Search - Related Models

In the "Advanced Search" I would like people to be able to enter details about a related model to narrow the search, as well as the normal criteria. How can I get that to work?

For example, if I have User and Person models, related as you might expect, I would like people to be able to search by username (a column of the User database table) and also lastname (a column of the Person database table).

try this

class User extends CActiveRecord {

  //lets last_name is from related table

	public $last_name;

	public function rules() {

    	return array(

        	// The following rule is used by search().

        	array('id,status,last_name', 'safe', 'on' => 'search'),



 	public function search() {

    	$criteria = new CDbCriteria;

   	//lets person is ur related table relation name

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

    	$criteria->compare('person.last_name', $this->last_name, true);

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

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

    	return new CActiveDataProvider($this, array(

                	'criteria' => $criteria,




Thanks, I’ll try that out in a minute. So clicking the “Search” button in the “Advanced search” area of the page results in the model’s search() method being run? I didn’t know that. I’ve no idea if that is documented anywhere, or whether I’ve just missed it.

If I use $this->person->lastname it complains that $this->person is not an object (why?). If I instead use Person::model() it runs, but doesn’t work (entering a last name and clicking “Search” has no effect on the results).

can u plz paste ur gridview code ?

u can write the custom query on actionserch function

for example

 if(isset($this->user_id) && !empty($this->user_id)){

            $criteria = new CDbCriteria;

            $criteria->select = 't.*, tu.* ';

            $criteria->join = ' LEFT JOIN `users` AS `tu` ON t.user_id =';













                                         array('class' => 'CCheckBoxColumn',







try this link

Can you explain that a bit more please?

It doesn’t make any difference.

Hi Heyho,

The wiki Lal Zada has posted is the “must read” document when you work with the relation in CGridView and CActiveDataProvider. I believe that it’s the most suitable answer to your question. It should work. Just read it and try it.

And if you still have a problem, ask questions with your code. Saying just “doesn’t work” will never work.

You seriously expect me to post all my code? Nobody’s going to read all that and since I don’t know where the problem might be I can hardly cut it down to the appropriate parts. I think I have more chance of someone making suggestions as to what the problem might be based on them having had the same experience.

What do you mean by "all my code"?

The relevant parts should be very small. The controller action, search method in the model, and the view. And you must be able to cut them down to the minimal code by removing the parts that are apparently unrelated.