【发布时间】:2011-10-24 02:50:54
【问题描述】:
我一直在尝试使用 foreach 在同一个表中显示来自不同模型的不同数据。这是模型之间的关系
客户有很多工作
工作属于客户
Job hasMany Jobtask
Jobtask 属于Job
Jobtask 有许多 Jobtasksvehicle
Jobtasksvehicle 属于 Jobtask 和 Vehicle
Vehicle hasMany Jobtasksvehicle
这是我的控制器
function viewsch($id = null) {
$jobs = $this->Jobtask->find('all', array(
'contain' => array('Customer',
'Job' => array( 'conditions' => array('Job.id =' => 'Jobtask.job_id')),
'Jobtasksvehicle'=> array( 'conditions' => array('Jobtasksvehicle.vehicle_id = Vehicle.id'))
)));
$this->set(compact('jobs'));
}
这是我的看法
<div class="jobs index">
<h2><?php __('Jobs Summary');?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Job Id</th>
<th>Jobtasks ID</th>
<th>Customer</th>
<th>Vehicle ID</th>
</tr>
<?php
$i = 0;
foreach ($jobs as $job):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $job['Job']['id']; ?> </td>
<td><?php echo $job['Jobtask']['id']; ?> </td>
<td><?php echo $job['Customer']['full_name']; ?> </td>
<td><?php echo $job['Jobtasksvehicle']['vehicle_id']; ?> </td>
</tr>
<?php endforeach; ?>
</table>
<?php echo debug($job); ?>
</div>
我使用了可包含的行为,当我调试 $job 时,我只从 jobtask、job 和 jobtasksvehicle 获取数据,而不是从客户那里获取数据。但是jobtasksvehicle中没有数据。我已经将 jobtasksvehicle 分配给 jobtask。在显示中,仅显示作业和作业任务,其余错误显示未定义索引:客户和未定义索引:车辆 ID。
请有人帮助我。这对我的项目非常重要。谢谢。
注意:如果是SQL,我想显示的内容是这样的。
从 CUSTOMERS c、JOBS j、JOBTASKS t、JOBTASKSVEHICLES tv 中选择 c.full_name、j.id、t.id、tv.vehicle_id,其中 tv.jobtask_id = t.id 和 t.job_id = j.id 和 j。 customer_id = c.id;
【问题讨论】: