【问题标题】:cakephp getting data from different modelcakephp 从不同的模型获取数据
【发布时间】: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


【解决方案1】:

使用 Ross 提到的 Containable 行为是最好的方法。添加到您的工作模型:

var $actsAs = array('Containable');

在你的控制器中:

$job = $this->Job->find('first', array(
    'conditions' => array('Job.id' => $id),
    'contain' => array(
        'Jobtask' => array(
            'Jobtasksvehicle' => array(
                'Vehicle'
             ),
         ),
    ),
));
$this->set(compact('job'));

请参阅Containable documentation 以获取更多帮助。我将把视图中数据的争吵留给你。

【讨论】:

  • 我已经按照你发布的内容完成了,我有工作、工作任务、客户详细信息显示,但我无法显示车辆详细信息。我试过这样但它不起作用 Html->link($job['Vehicle']['vehiclemodel'], array('controller' => 'vehicles', 'action ' => 'view', $job['Vehicle']['vehiclemodel'])); ?>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多