0

我在 cakephp 2.1 应用程序标签、类别、产品中有 3 个模型。

产品通过 hasOne 关系与 Category & Label 相关联。

使用可包含或可链接来实现以下数据结构的最有效方法是什么。

[Label]
    [Category]
        [Product]

基本上我想按类别对产品进行分组,按标签对类别进行分组。

我能想出的唯一解决方案是通过检索产品来修改 afterFind 样式的数据

$this->Product->find('all', array(
    'contain' => array(
        'Category',
        'Label
);

然后使用 foreach 对结果进行迭代以重新格式化数据结构以获得所需的结果。

4

1 回答 1

0

我不太确定我是否理解正确,但根据我的理解,这就是你应该做的。

在各自的模型中

Label hasMany Category; // label.php
Category belongsTo Label; // category.php
Category hasMany Product; // category.php
Product belongsTo Category; // product.php

在您要获取这些关联的产品控制器中。

$this->Product->Label->recursive = 2;
$this->Product->Label->find('all');

结果结构将是

[Label]
   [Category]
      [Product]
于 2012-04-03T10:32:02.993 回答