Search for MANY_MANY value in CGridView

Great topic, very helpful.

You can also supply an array of assigned categories for


CHtml::checkBoxList()

which checks coresponding checkboxes, and then in the action use


$model->setRelationRecords()

supplying relation name and checkboxlist data from the form. And after running


$model->save()

CSaveRelationBehavior takes care of the database. So far havent seen any bugs with this method.

Just wondering right now if this is better than using combined table model.

I’m following this topic and trying to doing the same, but for some reason, it doesn’t work when I’m trying to filter the CGridView by the many_many relation.

Model Book.php




public $assignedKeywords;

 

public function relations() {

        return array(

            'memberOwnBooks' => array(self::HAS_MANY, 'MemberOwnBook', 'book_id'),

            'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),

            'keywords' => array(self::MANY_MANY, 'Keyword', 'book_has_keyword(book_id, keyword_id)'),

        );

    }

 

public function getRelatedKeywordNames() {

       $out = CHtml::listData($this->keywords,'id','name');

       return implode(', ', $out);

    }

 

public function searchLibrary() {

        $criteria = new CDbCriteria;

 

        $criteria->condition='topic_id!=:topic_id';

        $criteria->params=array(':topic_id'=>1);

 

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

        ...

 

        $criteria->compare('keyword_id', $this->assignedKeywords);

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

        $criteria->together=true;

 

        return new CActiveDataProvider($this, array(

                'criteria' => $criteria,

        ));

    }



View library.php




$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'book-grid',

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

    'filter' => $model,

    'columns' => array(

     ...

array(

                'name'=>'assignedKeywords',

                'filter'=>CHtml::listData(Keyword::model()->findAll(array('order'=>'name ASC')),'id','name'),

                'type'=>'html',

                'value'=>'$data->relatedKeywordNames',

                ),

...



BookController.php




public function actionLibrary() {

        $model = new Book('searchLibrary');

        $model->unsetAttributes();

 

        if (isset($_GET['Book']))

            $model->setAttributes($_GET['Book']);

 

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

            'model' => $model,

        ));

    }



Any toughs? Thanks, Pablo.

I solved it using the ‘safe’ validator:


public function rules()

    {

        return array_merge(parent::rules(), array(

            array('assignedKeywords','safe'),

        ));

    }

A lot of thanks Pablo, you make my day !

Cheers,

pod2g

Many thanks Domba such an awesome sharing !

I followed the topic and it works amazingly.

However I would have a question. With the filter of my MANY_MANY relation, when there is nothing type in, or selected, it gets me records which have no value for this field instead of giving me all records anyway their value without filter.

I don’t succeed to manage the fact to show all records anyway their field when nothing is selected in filter…

Because actually, nothing selected in filter gives a filter=’’ instead of no filter.

Can you help ?