I've been trying to display the data from different model in the same page but still can't get it working. This is the relationship betweens models
Customer hasMany Job
Job belongsTo Customer
Job hasMany Jobtask
Jobtask belongsTo Job
Jobtask hasMany Jobtasksvehicle
Jobtasksvehicle belongsTo Jobtask and Vehicle
Vehicle hasMany Jobtasksvehicle
This is my job controller
function sch($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid job', true));
$this->redirect(array('action' => 'index'));
}
$job = $this->Job->find('first', array(
'conditions' => array('Job.id' => $id),
'contain' => array('Customer',
'Jobtask' => array(
'Jobtasksvehicle' => array(
'Vehicle'
),
),
),
));
What I've to do is display data from Customer, Job, Jobtask, Jobtasksvehicle, Vehicle models. I can get the data display from Customer, Job and Jobtask but not from Jobtasksvehicle and Vehicle models. I want to display each vehicle (vehiclemodel) assigned to each jobtask.
This is where I want to display jobtask and Vehicle models in the same table.
<?php
$i = 0;
foreach ($job['Jobtask'] as $jobtask):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $jobtask['id'];?></td>
<td><?php echo $jobtask['job_id'];?></td>
<td><?php echo $jobtask['rate_id'];?></td>
<td><?php echo $jobtask['type'];?></td>
<td><?php echo $jobtask['date'];?></td>
<td><?php echo $jobtask['starttim开发者_C百科e'];?></td>
<td><?php echo $jobtask['timeband'];?></td>
<td><?php echo $jobtask['settlement'];?></td>
<td><?php echo $job['Vehicle']['vehiclemodel'];?></td>
</tr>
<?php endforeach; ?>
</table>
?>
Everything seems to be fine, you need to put the find in recursive = 3, like this
$job = $this->Job->find('first', array(
'conditions' => array('Job.id' => $id),
'recursive' => 3,
'contain' => array('Customer',
'Jobtask' => array(
'Jobtasksvehicle' => array(
'Vehicle'
),
),
),
));
but still the containable behaviour should calculate the recursive automatic, Make sure you are loading the containable bahaviour.
Check the book out about containable behaviour
Also check if the sql queries are being done, if the sql queries appear you may need to specify the fields (you may want to use Job.* o Vehicle.* to get all fields...)
精彩评论