【问题标题】:cakephp using foreach to display different data from different modelcakephp 使用 foreach 显示来自不同模型的不同数据
【发布时间】: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']; ?>&nbsp;</td>

    <td><?php echo $job['Jobtask']['id']; ?>&nbsp;</td>     

    <td><?php echo $job['Customer']['full_name']; ?>&nbsp;</td>     

    <td><?php echo $job['Jobtasksvehicle']['vehicle_id']; ?>&nbsp;</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;

【问题讨论】:

    标签: cakephp foreach


    【解决方案1】:

    如果你改变了,你能发布 debug($job) 的输出吗

     <td><?php echo $job['Jobtasksvehicle']['vehicle_id']; ?>&nbsp;</td>
    

    <td><?php echo debug($job); ?>&nbsp;</td>
    

    这将有助于显示数据是否正在被检索但以意外的方式存储在 $job 数组中。

    【讨论】:

    • 我不明白你想说什么。我只是为了知道 $job 检索到了哪些数据而进行了调试。
    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多