I am trying to create a CMS with Nodes as the main model. Each Node belongsTo a NodeType , and each Node can be associated with any / any other Node .
So, I thought this caused HABTM:
//Node model public $hasAndBelongsToMany = array( 'AssociatedNode' => array( 'className' => 'Node', 'foreignKey' => 'node_id', 'associationForeignKey' => 'associated_node_id', 'joinTable' => 'node_associations' ) );
The problem is that it seems that the only way to use it is that I have two lines for each association.
An example with only one association string:
Knots
- ER (id = 1)
- George Clooney (id = 2)
One row in the connection table describing the relationship between these two nodes:
- 'node_id' = 1
- 'associated_node_id' = 2
Now - if I request a TV show and save it. Actor nodes:
$nodes = $this->Node->find('all', array( 'conditions' => array( 'Node.node_type_id' => '645'
This works and I get ER → George Clooney.
But what if I want to pull out all the shows that George Clooney is in?
$nodes = $this->Node->find('all', array( 'conditions' => array( 'Node.node_type_id' => '239'
This does not work because it looks for the George Clooney identifier in the "node_id" field, and the ER identifier should be in the "associated_node_id" field - when in reality they are reversed.
The only solution I was thinking of was to keep two lines for the EVERY association. But that seems redundant. But then I have to come up with some kind of ordinary something that guarantees that every duplicate will be synchronized every time the association is saved or deleted ... etc. - and it looks like a big can of worms.
Is there something I am missing?