【发布时间】:2011-10-09 11:04:20
【问题描述】:
我是 cakephp 新手,在构建应用程序时遇到问题。在我的应用程序中,我有名为 Customer、Job、Jobtask、Jobtasksvehicle 和 Vehicle 的模型。他们的数据库关系是Customer->Job->Jobtask->Jobtasksvehicle->Vehicle。 我有一个名为工作表的页面,在该工作表中我必须显示客户姓名、工作 ID、工作任务和车辆名称。我可以获得客户姓名、工作 ID 和工作任务显示,但我不知道如何在其中显示车辆名称。谁能帮帮我。
这是页面的模型
function sch($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid job', true));
$this->redirect(array('action' => 'index'));
}
$this->set('job', $this->Job->read(null, $id));
我想在这个表格中显示车辆名称
<div class="related">
<?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['starttime'];?></td>
<td><?php echo $jobtask['timeband'];?></td>
<td><?php echo $jobtask['settlement'];?></td>
<td><?php echo $job['Jobtasksvehicle']['vehicle_id'];?></td>
<td class="actions">
<?php echo $this->Html->link(__('Allocate Vehicle', true), array('controller' => 'jobtasksvehicles','action' => 'add', $jobtask['id'])); ?>
<?php echo $this->Html->link(__('View', true), array('controller' => 'jobtasks', 'action' => 'view', $jobtask['id'])); ?>
<?php echo $this->Html->link(__('Edit', true), array('controller' => 'jobtasks', 'action' => 'edit', $jobtask['id'])); ?>
<?php echo $this->Html->link(__('Delete', true), array('controller' => 'jobtasks', 'action' => 'delete', $jobtask['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $jobtask['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
这是模型之间的关系
客户有很多工作
工作属于客户
Job hasMany Jobtask
Jobtask 属于Job
Jobtask 有许多 Jobtasksvehicle
Jobtasksvehicle 属于 Jobtask 和 Vehicle
Vehicle hasMany Jobtasksvehicle
【问题讨论】:
-
发布您的确切关系($hasMany、$belongsTo...)。这不是一个好方法,因为您将返回潜在的大量数据。您应该将Containable behaviour 视为更好的选择,只选择您需要的。
-
我已经编辑了模型之间的关系。
标签: cakephp