Clistview With Many To Many

Hi I have a problem doing my dataProvider to show many to many logic.

Here my tables:

Client

id

name

Bought

id

clientId

productId

Product

id

name

I want to have all the product bought by the client

so I try to code the actionView of the client:

ClientController.php




public function actionView($id)

	{

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

                'criteria' => array(

                    'with' => array('clients'),

                )

                    )

            );

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

			'model'=>$this->loadModel($id),

                        'travelers' => $productsDataProvider,

		));

	}



view.php




<h1>View Client#<?php echo $model->id; ?></h1>


<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'name'

	),

)); ?>

<br>

<h1>Products</h1>


<?php $this->widget('zii.widgets.CListView', array(

        'dataProvider'=>$productsDataProvider,

        'itemView'=>'/_products',

)); ?>



_products.php




<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />




</div>



I know to do it with sql, but not yet with the yii logic:




SELECT p.id , p.name FROM product p

JOIN bougth b ON b.clientId = 1



But it didn’t work.

Thanks for help

Hello all,

Here my actionView code :




public function actionView($id)

	{

            $productsDataProvider = new CActiveDataProvider('Products', array(

                'criteria' => array(

                    'with' => array('clients'=>array(

                        'condition'=>"clientId=$id")),

                )

                    )

            );

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

			'model'=>$this->loadModel($id),

                        'productsDataProvider' => $productsDataProvider ,

		));

	}



The problem, it show all the product ???

Thanks

EDIT RESOLVE :




public function actionView($id)

	{

            $productsDataProvider = new CActiveDataProvider('Products', array(

                'criteria' => array(

                    'with' => array('clients'=>array(

                        'condition'=>"clientId=$id",'together'=>true)),

                )

                    )

            );

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

			'model'=>$this->loadModel($id),

                        'productsDataProvider' => $productsDataProvider ,

		));

	}



Because of the together.

But it’s the best way ?

Thanks