Yii Framework Forum: [Solved] Multiple Model Search For Clistview - Yii Framework Forum

Jump to content

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

[Solved] Multiple Model Search For Clistview Rate Topic: -----

#1 User is offline   smayzes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 12
  • Location:Vancouver

Posted 24 September 2013 - 02:06 PM

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.yiiframew...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:

Quote

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'instructorToken.first_name' in 'where clause'


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?
0

#2 User is offline   nineinchnick 

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

Posted 24 September 2013 - 02:54 PM

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

#3 User is offline   smayzes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 12
  • Location:Vancouver

Posted 24 September 2013 - 03:31 PM

View Postnineinchnick, on 24 September 2013 - 02:54 PM, said:

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. :)
0

#4 User is offline   nineinchnick 

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

Posted 25 September 2013 - 02:10 PM

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

#5 User is offline   smayzes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 12
  • Location:Vancouver

Posted 25 September 2013 - 03:53 PM

View Postnineinchnick, on 25 September 2013 - 02:10 PM, said:

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!
0

#6 User is offline   nineinchnick 

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

Posted 26 September 2013 - 10:13 AM

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