如何从数组中检索值 - Cakephp

时间:2009-09-14 06:41:10

标签: cakephp

我正在使用名为(id,form_id,title)

的报告的表格

和另一个表格(id,name)

在我的reports.ctp中 我通过使用

从表格中获取所有报告标题
function reports()
{
    $allreports= $this->Report>find('all',
                                    array('conditions'=>array('Report.user_id'=>$userId),
                                          'group' => array('Report.report_id')));

    foreach($allreports as & $report):
        $report['Report']['formname'] = $this->Form->find('all',
                array(
                    'fields'=>array('Form.name'),
                    'conditions'=>array('Form.id'=>$report['Report']['form_id'])));
    endforeach;

    $this->set('allreports', $allreports);
}

我使用foreach从Forms表中找到相应的Form名称..

reports.ctp

我希望报告标题和表格名称一一对应,如

Report1 - Form name - Form1

Report2 - Form name - Form2

Report3 - Form name - Form1

我使用

尝试了上述内容
          <div> <a href="#">
                            Reportname:    <?php echo $report['Report']['title'];?>
       </a>
       <a href="#">   Form name <?php echo $report['Report']['formname'];?></a>
         </div>

但我得到像

那样的数组
Report1 - Form name - Array

Report2 - Form name - Array

Report3 - Form name - Array

怎么办?请建议我..

1 个答案:

答案 0 :(得分:0)

$report['Report']['formname'] = $this->Form->find(...) 将返回一个数组。这就是为什么它说“阵列”。

如果你要继续使用你正在使用的逻辑......尝试更像......的事情。

foreach($allreports as & $report):
    $temp = $this->Form->read('name',$report['Report']['form_id']);

    $report['Report']['formname']=$temp['Form']['name']
endforeach;

但是啊..你也可以选择使用 CakePHP 内置关系功能的路线,如deceze所说。这肯定是更清洁的方式。

相关问题