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>"));
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,
));
}