On Sat, Jan 8, 2011 at 6:34 PM, tesla100 <[email protected]> wrote:
> Hi folks,
>
> Here is my problem:
>
> Let's say I have a database called 'Outfits'.  There are two tables,
> 'pants' and 'shirts'.
>
> pants and shirts have a many to many relationship.
> pants has id and title fields.
> shirts has id and title fields.
>
> There needs to be a many to many relationship. In Cake terms,
> hasAndBelongsToMany (HABTM).  A record in pants can be associated with
> many shirts and a shirt can be associated with many pants.
>
> Hence, a cross referencing table exists called pants_shirts with id,
> pant_id, shirt_id.
>
> You with me so far?
>
> Okay.  Lets say when I add a pant entry, I have a check box for each
> shirt entry in the shirts table.  I check all the shirts I want.  Now
> lets assume when I want to retrieve this information, I want it back
> in the same order as I checked them in.  So using Jquery, I implement
> sortable on the check boxes of the shirts on the add pants page.  This
> allows me to change the order of the pants.  When the form has been
> submitted, I use the $key $value pairs to get the ordinal position and
> know the order.
>
> Now my question is - how or where should I store this value?  My
> solution is to add a field to the cross referencing table called
> position.  So when making a pants entry, in the cross table called
> pants_shirts,
> I get something like this
> id   pant_id  shirt_id    position
> 0     1             5             0
> 1     1             8             1
> 2     1             12           2
>
> When implementing this, I let cake php add an entry to the pants table
> and the additional entries that go with it in the pants_shirts table.
> THEN, I update the pants_shirts table where pants.id =
> pants_shirts.pants_id and update the positions.  I do this update by
> doing $this-Pant->sql('sql to update here')
>
> This seems to work so far in my coding process.  The reason I post it
> on here is, it seems like a bad practice to add the position field in
> the cross referencing table.  Any ideas or suggestions?

No, that's fine. You might also be interested in "hasMany through":

http://book.cakephp.org/view/1650/hasMany-through-The-Join-Model

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" 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?hl=en

Reply via email to