Many-Many List

Hi,

I have an other problem,

How to create a list with only the product that doesn’t exist on many-many table ?

Here the table

Client

id

name

Bought

id

clientId

productId

Product

id

name

category

And I want to have a dropdownlist with only the product that the client didn’t bought and with a defined category.

My form:




<?php echo CHtml::activeDropDownList($model,'productId',  $product->getUnboughtProductList($catId)); ?>



And my try:

Product.php




public function getUnboughtProductList($catId){

        return CHtml::listData(Product::model()->findAll("catId = $catId"),'id','name');

    }




The problem, I have also the product bought, how to add a condition that he cannot be on Bought table ?

Thanks




public function getUnboughtProductList($catId){

         $criteria = newCDbCriteria();

         $criteria->addCondition("catId = $catId");

         //Put more conditions as you want.

        return CHtml::listData(Product::model()->findAll($criteria),'id','name');

    }




Or

  

Product::model()->findAll(array("condition"=>"id=1 AND <youcondition>"));




i hope you got the point here.

Hi

First make a list of all the products that the client already bought.

Then get all products Not In that list.

Use $criteria->addNotInCondition in the product_model.

It creates sql that do something like this:

select product where product.id NotIn [select bought.productId where bought.clientId = client.id]

or

select product where product.id NotIn [list of products that client already bought]

Controller:




$product_model->clientID = $client_model->id;

... 

render view with list, passing it product_model.



view:




list uses product_model->productsNotBought()



product_model:




public 	$clientID = null;


public function productsNotBought()

{

	/*findColumn is in CAdvancedArFindBehavior. 

	It extracts a single column from the models.

 	Maybe the latest Yii can do this. */


	$boughtProductsList = bought::model()->findColumn(

		'productId', 'clientId = '. $this->clientID);

		

	$criteria->addNotInCondition('id', $boughtProductsList); /* id = Product.id */


	return new CActiveDataProvider($this, array(

		'criteria'=>$criteria,

	));

}



I would go this route, too.

Thanks very much, I will try right now :D

EDIT : Thanks it works like a charm :)

Cool. :rolleyes: