Problems with Many to Many relation in a CGridView

Table users

id

name

Table roles

id

name

Table user_roles

uid (fk users.id)

rid (fk roles.id)

I want to search only users with a specific role.

In the users model:




    public function relations() {

        return array(

            'roles' => array(self::MANY_MANY, 'Roles','user_roles(uid, rid)'),

        );

    }


...


public function customersList() {

        $criteria = new CDbCriteria;

        $criteria->with = array(

            'roles'=>array(

                'select'=>false,

                'joinType'=>'INNER JOIN',

                'condition'=>'roles.name="customer"',

            ),

        );


        $criteria->compare('t.id',$this->id,true);

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }



In the controller:


public function actionCustomers() {

        $model = new Users('customersList');

        $model->unsetAttributes();

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

                $model->attributes=$_GET['Users'];


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

                'model'=>$model,

        ));

    }

In the view:


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

	'id'=>'customers-grid',

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

        'filter'=>$model,

	'columns'=>array(

		'id',

		array(

			'class'=>'CButtonColumn',

		),

	),

));

The pager display the correct number of rows (only 1), but two lines appear in the table:

http://ge.tt/api/1/files/1itygaL/0/blob/x675?noinc=1

Where am I doing wrong?

thank you

Solved adding:




'together'=>true,



now is




public function customersList() {

        $criteria = new CDbCriteria;

        $criteria->with = array(

            'roles'=>array(

                'select'=>false,

                'joinType'=>'INNER JOIN',

                'together'=>true,

                'condition'=>'roles.name="customer"',

            ),

        );


        $criteria->compare('t.id',$this->id,true);

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }



That is Great Nicola :)

Thanks for your this update.I was not aware with


'together'=>true,

I recommend the use of Giix extension.

Happy Coding.

cool extension!