[Solved] Multiple Model Search For Clistview

Hi there,

I’m a bit stuck on providing a solution for searching records in a CListView.

I’ve followed this wiki page: http://www.yiiframework.com/wiki/229/filter-search-with-clistview/

In my code I have:

LessonsController.php




	public function actionIndex() {

		$model = new Lessons('search');

	//  Clear any default values

		$model->unsetAttributes();


		$this->render('index',array(

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

		    'model'=>$model

		));

	}



Lessons.php




    public function search() {

    //  Get the typed query string

	$query = Yii::app()->request->getQuery('query');


        $criteria = new CDbCriteria;


    //  Package Name

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

    //  Quantity Name

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

    //  Package Id

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


    //  Do we have a search query string?

	if ( !empty($query) ) {

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

		$criteria->compare( 'instructorToken.first_name', $query, true );

		$criteria->together = true;

	}


        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

        ));

    }



As you can see I’m trying to use the with method to search another table for the first name. I have a relation which is as follows:




    public function relations() {

        return array(

		'instructorToken' => array(self::BELONGS_TO, 'Users', 'instructor_token'),

        );

    }



I get an error that states:

Now I’m confused as that wiki page states how to do the with comparison of the related model. Also reading the documentation of the with() method says it requires the key from the relations method.

Anyone have any idea what I’m doing wrong or missing?

What database are you using? I know that in PostgreSQL, when you want to use mixed case in table names or aliases, you have to properly quote them.

Also, when you add a relation to ‘with’ and want to reference it in a WHERE condition you need to enable eager loading by switching on the ‘together’ property.

This is in MySQL.

I have switched on the together property in my original code have I not? Forgive me if I’m confused there.

Thanks for response. :)

Oh sorry, haven’t noticed that.

Can you post full query that is executed? It should be in your logs if YII_DEBUG is on and db profiling is enabled.

So, I didn’t think to look at my logs to check the query. Good thing you mentioned that as I looked at the query and it was giving the using the model name for the alias of my joined table, so I thought I should change my code from:


$criteria->compare( 'instructorToken.first_name', $query, true );

I should have put:


$criteria->compare( 'Users.first_name', $query, true );

The whole time I thought I had to use the relation name as the alias and not the model name.

Thank you so much for the help!

It should use relation name as an alias to the table name. Maybe you’ve overriden some methods related to this in your model classes?