Cakephp 3: belongsToMany 包含或匹配条件?

Cakephp 3: belongsToMany contain or matching condition?

我的数据库的一部分看起来像 database part

类别使用树行为。

如何获得当前类别的制造商(生产商)产品? 我尝试包含和匹配,但我收到了重复的数据或生产者名称,但没有相关产品。

编辑:

$query = $this->Producers->find()->matching('Products.Categories',
    function ($q) {
        return $q->where(['Categories.id' => 18]); 
    }
);

结果:

Producent: Canon
-------------------------------------------
| ID | Name        | Barcode              |
-------------------------------------------
| 1  | EOS 1000D   |                      |
-------------------------------------------
| 18 | Camera      |                      |
-------------------------------------------
| 23 | 18          |                      |
-------------------------------------------

第一行(id = 1)是我需要的。 现在我必须从结果中删除: 第二行 (id = 18) 这是来自 table 个类别的类别 ID, 第三行 (id = 23) - 来自 Products_Categories table。

完成。有工作查询:

$query = $this->Producers->find()
        ->select(['Producers.id','Producers.name', 'Products.id',   'Products.name'])
        ->matching(
            'Products.Categories', function ($q) use ($categoryId){
                return $q->where(['Categories.id' => $categoryId]);
            }
        );