0

I'm stuck with something that is killing me! So, I have

Order HABTM Product (with a products_order join table)

The join table looks like id | product_id | order_id | quantity | price

At the moment, when I'm making a find call I have the following result

[...]
'Product' => array(
    (int) 0 => array(
        'id' => '137',
        'category_id' => '2',
        'restaurant_id' => '10000',
        'title' => 'Salade Quan',
        'description' => 'Crudités Chinoises, Porc, Crabe, Crevettes',
        'price' => '9',
        'tva_id' => '2',
        'img' => null,
        'img_small' => '',
        'img_medium' => '',
        'maj_img' => '2012-10-24 15:38:56',
        'ProductsOrder' => array(
            'id' => '35415',
            'product_id' => '137',
            'order_id' => '15417',
            'quantity' => '1',
            'price' => '9',
            'tva_id' => '2',
            'meal' => null
        )
    ),
[...]

But I would like to add more data on a specific product of an order. Typically, a meat product can have a side order / one or multiple sauces ... So, I thought about adding a hasMany relation to a model that represent the join table.

I created the model ProductsOrder which represent the products_order join table. I created a new table extras_order and his model and added a hasMany relation to ProductsOrder model.

And I call the find method like that :

 $q = $this->Order->find('first', array(
        'conditions' => array(
            'Order.id' => $id,
        ),
        'contain' => array(
            'Product.Restaurant' => array('title'),
            'State' => array('title'),
            'Coursier' => array('full_name'),
            'Product',
            'Product.restaurant_id != ' . AppController::ID_RESTAURANT,
            'AddressB' => array('Cp'),
            'AddressD' => array('Cp'),
            'Customer',
            'ProductsOrder'
            ))
    );

But it is not working which make me thing this is not a good solution. I only get

 [...]
'ProductsOrder' => array(),
 [...]

What do you think ?

Is it a good approach for you ? Where to store additional data of an order ?

Thanks in advance !

EDIT : I finally end up by adding a field in the join table and thinking about adding my extras in json.

azerto00
  • 1,001
  • 6
  • 18
  • Looks like you need to revisit the 'Containable' section of the book. There are a number of things there that are not correct. – Dave Oct 11 '13 at 13:40
  • And normally, in the case of multiple sauces for an order, you would create a 'sauce' table, which contains all the possible sauces and a join table 'order_sauce' with an order_id and sauce_id. Each order can have multiple sauces then (MANY to MANY relation) – davey Oct 11 '13 at 13:45
  • My 'Containable' could be wrong but I'm talking about relations between Model ... And davey you're right for multiple sauces, but it could be sauces, side order or anything else. I can't create a table for each different extra. Maybe I didn't explain it right – azerto00 Oct 13 '13 at 12:50

0 Answers0