smayzes Posted September 24, 2013 Share Posted September 24, 2013 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? Link to comment Share on other sites More sharing options...
Edward Posted September 25, 2013 Share Posted September 25, 2013 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 Link to comment Share on other sites More sharing options...
smayzes Posted September 25, 2013 Author Share Posted September 25, 2013 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. Link to comment Share on other sites More sharing options...
smayzes Posted September 25, 2013 Author Share Posted September 25, 2013 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! Link to comment Share on other sites More sharing options...
Edward Posted September 26, 2013 Share Posted September 26, 2013 Glad you got it worked out, thanks for sharing your solution with us. Link to comment Share on other sites More sharing options...
Larry Posted September 27, 2013 Share Posted September 27, 2013 Excellent. Thanks for sharing that. Link to comment Share on other sites More sharing options...
Recommended Posts