Hi everyone
I am kinda new to yii and I'm having problem with CGridView filtering.
I have a CGridView with list of problems. Each problem can have 1 or more skills and there are many skills as well so relation between Problems and Skills is many to many.
In my view a managed to display all skills associated with each problem but now I don't know how to filter them (filtering from dropdown where if I select skill only problems with this skill would be in table)
This is my CGridView:
Just to note everything works fine except skill filter (and it doesn't even throw exception it just won't filter)
Here is my Problem model (I included only functions important to this case)
I think there is something wrong with criterias. Any ideas what's wrong and how to fix it?
I am kinda new to yii and I'm having problem with CGridView filtering.
I have a CGridView with list of problems. Each problem can have 1 or more skills and there are many skills as well so relation between Problems and Skills is many to many.
In my view a managed to display all skills associated with each problem but now I don't know how to filter them (filtering from dropdown where if I select skill only problems with this skill would be in table)
This is my CGridView:
<?php echo CHtml::link('Create problem',array('/site/createProblem')); $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'problem-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'cssFile'=>Yii::app()->request->baseUrl. '/themes/'. Yii::app()->theme->name.'/css/table.css', 'columns'=>array( 'id', 'title', array( 'name'=>'id_subject', 'filter'=>CHtml::listData(Subject::model()->findAll(), 'id','abbreviation'), 'value'=>'$data->subject->abbreviation', ), array( 'name'=>'skill', 'filter'=>CHtml::listData(Skill::model()->findAll(), 'id','name'), 'value'=>'$data->getRelatedSkills()', ), array( 'name'=>'category', 'value'=>'$data->category', ), array( 'class'=>'CButtonColumn', 'deleteConfirmation'=>"js:'Do you really want to delete this problem?'", //'deleteButtonImageUrl'=>null, 'deleteButtonUrl'=>'Yii::app()->createUrl("site/deleteProblem", array("id"=>$data->id))', 'template'=>'{solve}{delete}', 'buttons'=>array ( 'solve' => array ( 'label'=>'Solve', 'url'=>'Yii::app()->createUrl("site/solveProblem", array("id"=>$data->id))', ), 'delete' => array ( 'visible'=>'$data->subject->currentUserSubject->id_role < "3"', ) ), ), ), )); ?>
Just to note everything works fine except skill filter (and it doesn't even throw exception it just won't filter)
Here is my Problem model (I included only functions important to this case)
class Problems extends CActiveRecord { public $skill; public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('title, description, id_subject', 'required'), array('id_subject, max_score', 'numerical', 'integerOnly'=>true), array('title', 'length', 'max'=>50), array('category', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, title, description, id_subject, category, max_score', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { return array( 'skills' => array(self::MANY_MANY, 'Skill', 'problem_skill(id_problem, id_skill)'), 'subject' => array(self::BELONGS_TO, 'Subject', 'id_subject'), 'solutions' => array(self::HAS_MANY, 'Solutions', 'id_problem'), ); } public function getRelatedSkills() { $out=CHtml::listData($this->skills,'id','name'); return implode(', ', $out); } public function search() { $criteria=new CDbCriteria; $criteria->compare('t.id',$this->id); $criteria->compare('description',$this->description,true); $criteria->compare('title',$this->title,true); $criteria->compare('category',$this->category,true); $criteria->compare('max_score',$this->max_score); $criteria->with = array('skills', 'subject','subject.currentUserSubject' => array('alias'=>'currentUserSubject')); $criteria->together = true; $criteria->compare('subject.id',$this->id_subject); //this is probably wrong $criteria->compare('skills.id',$this->skill); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); } }
I think there is something wrong with criterias. Any ideas what's wrong and how to fix it?