Sunday, January 18, 2015

Re: Recursive HABTM Associations?

Hi.
I know it's been a while since this was posted, but did you get any solution on this matter??
I'm just struggeling with a similar problem and could only "find" your post on "dreamincode-dot-net".
you'd rly help me alot if you could tell me how you solved it.

Am Mittwoch, 2. Februar 2011 09:45:46 UTC+1 schrieb KuroTsuto:
Hey All,

I'm working on a project wherein I would like to implement a model that hasAndBelongsToMany of itself. To better illustrate this, imagine a data structure composed of linking nodes not unlike that of a tree. Instead of one parent and many child nodes, however, my structure has many parent nodes and many child nodes. The sequence of the nodes is still important (i.e. I still require a separation of children and parents). I would like to retain the ability to transverse the structure by traveling to any parent or any child from the node at which I currently sit, and so I believe HABTM relationships to be the answer.

My attempt to solve this conundrum was to implement a node table and two join tables - children and parents - both of which taking the place of what would be conventionally named nodes_nodes tables. The "children" join table consists of two columns - 'node_id' and 'child_id' - as does the "parents" join table - 'node_id' and 'parent_id'.
 
And so my Node model holds the HABTM associations

'Child' => array(
'className' => 'Node',
'joinTable' => 'children',
'foreignKey' => 'node_id',
'associationForeignKey' => 'child_id'
) ,
'Parent' => array( 
'className' => 'Node',
'joinTable' => 'parents',
'foreignKey' => 'node_id',
'associationForeignKey' => 'parent_id'
)

This approach has got me far as the Bake utility generating controllers and views that provide the option to select multiple Nodes as parents and children while adding or updating, however the selected parent/children are not actually inserted into the join tables.

And so I ask: am I going about this in the right manner? If not, or if you have an alternate suggestion - I'm all ears! Please point me in the correct direction :)

Thank you very much for your time and consideration,
~KuroTstuo

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: