Many-Many Filter

Hi,

Trying to apply a filter to a ListView but pagination returns incorrect results - should be 12 items/page.

[center]----------------------------[/center][center]Product [/center][center] Id [/center][center]----------------------------[/center][center] |[/center][center] |[/center][center] |[/center][center] *[/center][center]----------------------------[/center][center]ProductAudience [/center][center] product_id [/center][center] audience_id [/center][center]----------------------------[/center][center] *[/center][center] |[/center][center] |[/center][center] |[/center][center]----------------------------[/center][center]Audience [/center][center] id [/center][center]----------------------------[/center]

Product





    public function relations()

    {

        return array(

            'audiences' => array(self::MANY_MANY, 'Audience', '{{product_audience}}(product_id, audience_id)'),

            ....

        );

    }



ProductController





    public function actionIndex(array $audience = array())

    {

        $criteria = new CDbCriteria();

        $criteria->order = 't.name';


        if (count($audience) > 0)

        {

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

            $criteria->together =  true;

            $criteria->addInCondition('audiences.id', $audience);

        }


        $dataProvider = new CActiveDataProvider('Product', array(

                'criteria' => $criteria,

                'pagination' => array(

                    'pageSize' => 12,

                )

            ));


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

            'dataProvider' => $dataProvider

        ));

    }



Try it out here - http://cakecravings…skelton.ca/shop - select a few categories and you’ll notice the number of items decreases with each Filter added. If ALL filters are added, a page only has 3 items/page.

Thanks!

Hi waterloomatt,

I think it is the normal result when you search by a MANY_MANY relation with pagination.

Would you please try the following? I hope it will give you a fairly good result, though it might not be exactly what you want:




    public function actionIndex(array $audience = array())

    {

        $criteria = new CDbCriteria();

        $criteria->order = 't.name';


        if (count($audience) > 0)

        {

            $criteria->with = array(

                 'audiences' => array('select' => false),

            );

            $criteria->together =  true;

            $criteria->addInCondition('audiences.id', $audience);

            $criteria->group = 't.id';

        }


        $dataProvider = new CActiveDataProvider('Product', array(

                'criteria' => $criteria,

                'pagination' => array(

                    'pageSize' => 12,

                )

            ));


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

            'dataProvider' => $dataProvider

        ));

    }



I’ve written a wiki on search by a HAS_MANY relation, and it’s almost the same with the MANY_MANY relation. Please take a look at it.

http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/