Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I'm getting this right, you've got the following relationships (hopefully in your models):</p> <pre><code>TableA hasMany TableB. TableA hasMany TableC. TableB belongsTo TableA. TableC belongsTo TableA. TableC belongsTo TableD. (might be hasOne) TableD hasMany TableC. (might be hasOne) </code></pre> <p>If you're using the Containable behaviour (I very much recommend it, and set it at app_model level for all models to inherit), I think you can do something like this...</p> <pre><code>$this-&gt;TableA-&gt;find( 'all', array( 'contain' =&gt; array( 'TableB', 'TableC' =&gt; array( 'TableD' ) ), 'conditions' =&gt; array(...), 'order' =&gt; array(...) ) ); </code></pre> <p>If you need to pick specific fields, then you'll need to specify them in the contain parameter, for example here I restrict TableB's returned fields:</p> <pre><code>$this-&gt;TableA-&gt;find( 'all', array( 'contain' =&gt; array( 'TableB' =&gt; array( 'fields' =&gt; array( 'field_1', 'field_2' ), ), 'TableC' =&gt; array( 'TableD' ) ), 'conditions' =&gt; array(...), 'order' =&gt; array(...) ) ); </code></pre> <p>The returned data should be like so:</p> <pre><code> [0] =&gt; array( [TableA] =&gt; array( [id] =&gt; 12, [name] =&gt; 'Foo' ), [TableB] =&gt; array( [id] =&gt; 23, [table_a_id] =&gt; 12, [name] =&gt; 'Bah' ), [TableC] =&gt; array( [id] =&gt; 45, [table_a_id] =&gt; 12, [table_d_id] =&gt; 67, [name] =&gt; 'Woo', [TableD] =&gt; array( [0] =&gt; array( [id] =&gt; 67, [table_a_id] =&gt; 12, [name] =&gt; 'Wah' ) ) ) ) </code></pre> <p>However, I've never done this where the nested table is the parent of the container (TableD and TableC), so it might not work, but it's probably worth a try.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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