Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf "HABTM" or "HasMany Through" concept confusion
    primarykey
    data
    text
    <p><strong>Bounty:</strong></p> <p>+500 rep bounty to a GOOD solution. I've seriously banged my head against this wall for 2 weeks now, and am ready for help.</p> <p><strong>Tables/Models (simplified to show associations)</strong></p> <ul> <li>nodes <ul> <li>id</li> <li>name</li> <li>node_type_id</li> </ul></li> <li>node_associations <ul> <li>id</li> <li>node_id</li> <li>other_node_id</li> </ul></li> <li>node_types <ul> <li>id</li> <li>name</li> </ul></li> </ul> <p><strong>General Idea:</strong></p> <p>A user can create node types (example "TV Stations", "TV Shows", and "Actors"...anything). If I knew ahead of time what the node types were and the associations between each, I'd just make models for them - but I want this to be very open-ended so the user can create any node-types they want. Then, each node (of a specific node-type) can relate to any other node of any other node-type.</p> <p><strong>Description and what I've tried:</strong></p> <p>Every node should be able to be related to any/every other node.</p> <p>My assumption is that to do that, I must have an association table - so I made one called "node_associations" which has <code>node_id</code> and <code>other_node_id</code>.</p> <p>Then I set up my association (using hasMany through I believe): (below is my best recollection of my set-up... it might be slightly off)</p> <pre><code>//Node model public $hasMany = array( 'Node' =&gt; array( 'className' =&gt; 'NodeAssociation', 'foreignKey' =&gt; 'node_id' ), 'OtherNode' =&gt; array( 'className' =&gt; 'NodeAssociation', 'foreignKey' =&gt; 'other_node_id' ) ); //NodeAssociation model public $belongsTo = array( 'Node' =&gt; array( 'className' =&gt; 'Node', 'foreignKey' =&gt; 'node_id' ), 'OtherNode' =&gt; array( 'className' =&gt; 'Node', 'foreignKey' =&gt; 'other_node_id' ) ); </code></pre> <p>At first, I thought I had it - that this made sense. But then I started trying to retrieve the data, and have been banging my head against the wall for the past two weeks.</p> <p><strong>Example Problem(s):</strong></p> <p>Lets say I have a the following nodes:</p> <ul> <li>NBC</li> <li>ER</li> <li>George Clooney</li> <li>Anthony Edwards</li> <li>Tonight Show: Leno</li> <li>Jay Leno</li> <li>Fox</li> <li>Family Guy</li> </ul> <p>How can I set up my data structure to be able to pull the all TV Stations, and contain their TV Shows, which contain their Actors (as example)? This would be SIMPLE with normal model setup:</p> <pre><code>$this-&gt;TvStation-&gt;find('all', array( 'contain' =&gt; array( 'TvShow' =&gt; array( 'Actor' ) ) )); </code></pre> <p>And then, maybe I want to retrieve all male Actors and contain the TV Show which contain the TV Station. Or TV Shows that start at 9pm, and contain it's actor(s) and it's station...etc etc.</p> <p>But - with HABTM or HasMany Through self (and more importantly, and unknown data set), I wouldn't know which field (node_id or other_node_id) the model is, and overall just can't wrap my head around how I'd get the content.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload