Reputation: 35983
I have a site develop in cakephp 2.0. I have a table "Product" related to property with hasMany, and HABTM to Product. A product can contain many product and that product can contain many product. I can have multiple level of product. To make this relation I have used this model (explained into this question: cakephp HABTM same model):
class Product extends AppModel {
public $name = 'Product';
public $useTable = 'products';
public $belongsTo = 'User';
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Ingredient' => array(
'className' => 'Product',
'joinTable' => 'ingredients_products',
'foreignKey' => 'product_id',
'associationForeignKey' => 'ingredient_id',
'unique' => true
)
);
}
I know how to retrieve product example: I have a product that contain other product I make a find with condition and I retrieve all data with their property like this:
$this->set('product',$this->Product->find('all', array(
'contain' => array(
'Property',
'IngredientProduct'
),
'conditions' => array('id' => $id)
)));
(IS ONLY AN EXAMPLE THE QUERY I DON?T KNOW IF IS GOOD DEPENDS OF MANY THINGS IS ONLY AN EXAMPLE)
But if I find 3/4 product inside my product and if findedproduct contain other product how can I do that? Because I don't know how many "levels" I have I can have 1 level or 6 level of product. A product contain many products, this finded products contain other products ecc.. How can I do that and retrieve for each product its property? It's a recursion query but the problem is that I don't know how many level I have. Some ideas? Thanks
Upvotes: 0
Views: 654
Reputation: 7525
Containable does not traverse so deep. I would advice to unbind your associations and join tables manually. Take a look here to see an example and at this cookbook chapter (you need to know SQL in your case). Or you can use TreeBehavior as Dave suggested.
Upvotes: 1
Reputation: 29141
Maybe you could/should go with something like - all products are just in "products" table, then you have a tree table that keeps the entire product hierarchy?
Upvotes: 0