Wednesday, April 17, 2013

HABTM Reciprocity

Whats the best way to achieve reciprocity between a HABTM self-referencing model?

For example:

I have a model called Zones it HABTM other Zones.  I have the join table setup and everything is working like other HABTM models.  But I want the reverse connection to also be established. So instead of adding one entry to the connections table it should add two; the second with the ids reversed.

Seems like there should be a better way than using afterSave and looping through the zones that were connected, adding the newly created ID as a connection, and re-saving.

class Zone extends AppModel {

/**
 * Use table
 *
 * @var mixed False or table name
 */

public $hasAndBelongsToMany = array(
'Connection' => array(
'className' => 'Zone',
'joinTable' => 'Zones_Connections',
'foreignKey' => 'zone_id',
'associationForeignKey' => 'connection_id',
'unique' => 'keepExisting'
)
);

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: