CakePHP多对多查找过滤

CakePHP多对多查找过滤,php,cakephp,join,find,has-and-belongs-to-many,Php,Cakephp,Join,Find,Has And Belongs To Many,我在产品和标签之间存在多对多关系 我需要检索按标签过滤的产品,所以我这样做: $this->Product->recursive = -1; $options['joins'] = array( array('table' => 'products_tags', 'alias' => 'ProductTag', 'type' => 'left', 'conditions' => array(

我在产品标签之间存在多对多关系

我需要检索按标签过滤的产品,所以我这样做:

$this->Product->recursive = -1;

$options['joins'] = array(
    array('table' => 'products_tags',
        'alias' => 'ProductTag',
        'type' => 'left',
        'conditions' => array(
            'Product.id = ProductTag.product_id'
        )
    )
);

$options['conditions'] = array('ProductTag.tag_id' => $filters);

$options['fields'] = array('Product.name', 'Product.filename', 'Product.url');

$products = $this->Product->find('all', $options);
如果
$filters
包含单个标记id,“find”将返回与此标记id相关的所有产品

但是,如果
$filter
为空或包含多个id,find将返回所有重复的产品,其标签数量与它们相同

例如,如果
$filters=1
,“find”返回包含
tag.id=1
的所有产品。好的

如果
$filters=array(1,2)
,“find”返回包含
tag.id=1
tag.id=2
的所有产品,如果产品包含tag.id=1和tag.id=2,则返回两次


即使产品有多个标记,“查找”也可能返回单个结果吗?

请看这里:谢谢Franck,它帮助了我。这与结果sql查询有关-您只需使用distinct:
选择distinct Product.*FROM…
不要使用连接返回结果。