2012-03-30 41 views
0

我有一个cakephp 2.1应用程序标签,类别,产品中的3个模型。Cakephp发现包含/可链接组

产品与类别&通过hasOne关系标签。

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

[Label] 
    [Category] 
     [Product] 

基本上我想按产品分类和按类别分类标签。

唯一的解决办法,我可以通过retriveing产品

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

拿出参与修改的一个afterFind风格数据,然后itterating在使用foreach结果重新格式化数据结构,以达到所需的结果。

回答

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]