0

I have 3 models in a cakephp 2.1 application Label, Category, Product.

Product is associated with Category & Label through a hasOne relationship.

What is the most efficient way using containable or linkable to achieve the following data structure.

[Label]
    [Category]
        [Product]

Basically I want to group Products by Categories and Categories by Labels.

The only solution I could come up with involved modifying a data in the afterFind style by retriveing products

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

and then itterating over the result using a foreach to reformat the datastructure to achieve the required result.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Chris
  • 73
  • 1
  • 6

1 Answers1

0

I am not quite sure if I understood that correctly but based on my understanding, here is what you should do.

In respective models

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

In product controller where you want to fetch these associations.

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

Resulting structure would be

[Label]
   [Category]
      [Product]
yetanotherse
  • 500
  • 3
  • 16