Hi,

I have a question regarding how to setup extra columns in a HABTM join
table, for instance, if I have the following DB structure:

products
- id (pk)
- name default 'my lovely pair of shoes'
- etc

product_sizes
- id (pk)
- name default 'medium'
- etc

products_sizes
- id (pk)
- product_id (fk)
- product_size_id (fk)
- quantity
- price

I can get a standard HABTM association working here fine, my problem
lies with adding the info into the DB for the extra columns: quantity
& price, in the join table.

Clearly I can't use a standard multi-selectTag, instead using a
combination of 4 fields for each (product, size, quantity, price).

To enable me to do this is I've created a new Model ProductPrice, and
then I loop through the form fields and manaually insert a new row for
each join:

$i = 0;
foreach ($this->data['Product']['sizes'] as $size_id)
{
    $data['ProductSize']['id'] = null;
    $data['ProductSize']['product_id'] = $product_id;
    $data['ProductSize']['size_id'] = $size_id;
    $data['ProductSize']['quantity'] = $this->data['Product']['quantities'][$i];
    $data['ProductSize']['price'] = $this->data['Product']['prices'][$i];
    $this->ProductSize->save($data);
    ++$i;
}

Now this works, but it's left me a little stumped, as it would be nice
if you could save a HABTM with more info than just an ID
automatically, but what I'm finding more tricky is getting the
quantity and price returned when I do a standard
$this->Product->findAll() etc.

Many thanks,

Jon
-- 


jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to