Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
smayzes

[Solved] Multiple Model Search For Clistview

Recommended Posts

Hi there, 
 
I'm a bit stuck on providing a solution for searching records in a 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: 

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 I do have 'first_name' in the Users model. Also reading the documentation of the with() method says it requires the key from the relations method, which I have. 
 
Anyone have any idea what I'm doing wrong or missing?

Share this post


Link to post
Share on other sites

I assume your HAS_MANY relationship is correct in your Users model. I could try to make a guess as to what your $query variable holds but i would most definitely be wrong, i would take a closer look at that and the following thread. Just to brush up on working with Related models in this situation.

 

http://www.yiiframework.com/forum/index.php/topic/20442-model-search-criteria-on-related-record/page__view__findpost__p__100148

Share this post


Link to post
Share on other sites

I assume your HAS_MANY relationship is correct in your Users model. I could try to make a guess as to what your $query variable holds but i would most definitely be wrong, i would take a closer look at that and the following thread. Just to brush up on working with Related models in this situation.

 

http://www.yiiframework.com/forum/index.php/topic/20442-model-search-criteria-on-related-record/page__view__findpost__p__100148

 

Hi, thanks for the link, I read through them and I did realize that my Users model did not have the HAS_MANY relationship. 

 

I added it like so: 

public function relations() {
    return array(
        'lessons' => array(self::HAS_MANY, 'Lessons', 'instructor_token'),
    );
}

I still get the error however and in brushing up on related models, I still end up stuck as what I understand from reading is that I should be able to run my criteria comparison using:

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

as 'instructorToken' is the relation and as I understand, I should be able to use as the alias it since I am loading that relation with the 'with()' method. 

 

'first_name' is in fact a record in the User model and I should be able to search it. Correct? (I hope)

 

I should mention my query variable is a string that user has entered into a text box. I'm hoping to allow the users to compare the $query to some of the records in the Lessons model and the Users model. 

Share this post


Link to post
Share on other sites

I should note that I beleive I figure this out.

 

Where I do: 

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

I should have put:

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

Users being the model name and not the relation name. 

 

Thank you so much for the help!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...