Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is nothing to join the found <code>Job/s</code> to a specific <code>Truck</code>.</p> <p><em>(I hope my explanation isn't too hard to understand, but CakePHP can be that way sometimes! imho)</em></p> <p>The <code>Jobs</code> are being attributed to the <code>Trucks</code> in an almost arbitrary way (my memory of Cake is that this can happen); the nature of the HABTM call attaches the <code>Job/s</code> to each of the 15 <code>Trucks</code>. This seems to be the current process from my point of view;</p> <ul> <li>Get all trucks.</li> <li>Get all jobs for those trucks where the date is <em>x</em>.</li> <li>[Problem 1] Attach the found <code>Jobs</code> to each <code>Truck</code> (that is your 15 trucks), but attached to every <code>Truck</code>.</li> <li>[Problem 2] Get all Leads/Employees related to that Job, <em>again</em> for each <code>Truck</code>.</li> </ul> <p>Problem 1: The source of the issue. You can see in the second query (SELECT <code>Job</code>...), where it uses the correct <code>truck_id</code>'s in the ON statement, but Cake cannot "join" these <code>Jobs</code> back into the right <code>Truck</code>, because it is a different query! So it joins the found jobs to each <code>Truck</code>.</p> <p>Problem 2: This is the 'real' problem, for Cake does not construct long JOIN statements, so there is no way to find the <code>Employees</code>/<code>Leads</code> only for those <code>Trucks</code> that you want. That is why it finds them for each Truck, this is because you are doing a <code>FindAll</code> on <code>Truck</code>.</p> <p>I hope that makes sense. You need to do a find all on <code>Jobs</code>, since that is the <em>'center'</em> of the query (pickup_date).</p> <pre><code>$this-&gt;loadModel('Job'); $whatev = $this-&gt;Job-&gt;find('all', array( 'contain' =&gt; array( 'Job' =&gt; array( 'Truck', 'Employee', 'Lead', 'conditions' =&gt; array( 'Job.pickup_date' =&gt; $date ) ) ) )); </code></pre> <p>CakePHP queries really only work when you find one/all of a certain model, it is better to start in the middle and work either side if you have a double HABTM relationship. If you wish to 'sort' by Truck, then you might have to write you're own query (model method) to accomplish the task yourself. In raw SQL this query may be easy, in abstracted PHP super-cake-ness this is difficult for CakePHP to allow.</p> <p>Happy baking, as they say!</p>
 

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