Tuesday, April 26, 2011

Re: Proper associations for a link table that links to the same table

I have to say I'm pretty impressed with cakePHP so far.

I really didn't think this would work, but the fact that I just did
what seemed logical and it worked like a charm just made cake awesome
in my book.

So I did confirm that you can indeed HABTM to yourself.

I created a link table called "parts_parts" which has id, part_id,
subpart_id and qnty.

In the Part model I associated the table to itself.

var $hasAndBelongsToMany = array('Parts'=>
array('className' => 'Part',
'joinTable' => 'parts_parts',
'foreignKey' => 'part_id',
'associationForeignKey' => 'subpart_id' ));

And it just worked! If I get a part, it also automatically gets all
it's sub parts linked in the parts_parts table.

Thanks cakePHP!

On Apr 23, 4:38 pm, turbo2ltr <turbo2...@gmail.com> wrote:
> I have "Parts" which has a type. The type can be a part or an
> assembly.
>
> A part is just that, a single part, whereas a part that's has a type
> of assembly is a collection of parts and/or other assemblies. So I
> need a table to link one records in Parts to one or more other records
> in the same Parts table.
>
> I have  AssyData which is a link table which has part_id and
> assy_part_id.  The problem is the table links two ID's in the same
> Parts table.  All the docs and tutorials seem to assume you are
> linking two different tables. Can CakePHP handle that or will I have
> to have a table for Parts and a separate table for Assemblies, then
> have the link table link those two tables?
>
> Thanks,
> Mike

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate