Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to nest joins with CakePHP?
    primarykey
    data
    text
    <p>I'm trying to behave. So, instead of using following SQL syntax:</p> <pre><code>select * from tableA INNER JOIN tableB on tableA.id = tableB.tableA_id LEFT OUTER JOIN ( tableC INNER JOIN tableD on tableC.tableD_id = tableD.id) on tableC.tableA_id = tableA.id </code></pre> <p>I'd like to use the CakePHP <code>model-&gt;find()</code>. This will let me use the <code>Paginator</code> too, since that will not work with custom SQL queries as far as I understand (unless you hardcode one single pagination query to the model which seems a little inflexible to me).</p> <p>What I've tried so far:</p> <pre><code>/* inside tableA_controller.php, inside an action, e.g. "view" */ $this-&gt;paginate['recursive'] = -1; # suppress model associations for now $this-&gt;paginate['joins'] = array( array( 'table' =&gt; 'tableB', 'alias' =&gt; 'TableB', 'type' =&gt; 'inner', 'conditions' =&gt; 'TableB.tableA_id = TableA.id', ), array( 'table' =&gt; 'tableC', 'alias' =&gt; 'TableC', 'type' =&gt; 'left', 'conditions' =&gt; 'TableC.tableA_id = TableA.id', 'joins' = array( # this would be the obvious way to do it, but doesn't work array( 'table' =&gt; 'tableD', 'alias' =&gt; 'TableD', 'type' =&gt; 'inner', 'conditions' =&gt; 'TableC.tableD_id = TableD.id' ) ) ) ) </code></pre> <p>That is, nesting the joins into the structure. But that doesn't work (CakePHP just ignores the nested <code>'joins'</code> element which was kind of what I expected, but sad.</p> <p>I have seen hints in comments on how to do subqueries (in the <code>where</code> clause) using a statement builder. Can a similar trick be used here?</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.
 

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