首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CakePHP在一个index.ctp中显示两个表的数据

CakePHP在一个index.ctp中显示两个表的数据
EN

Stack Overflow用户
提问于 2014-05-09 14:21:11
回答 1查看 673关注 0票数 0

我的companies/index.ctp中有以下代码:

代码语言:javascript
运行
复制
<div class="companies index">
    <h2><?php echo __('Company Details'); ?></h2>
    <table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('Id'); ?></th>
            <th><?php echo $this->Paginator->sort('Company Name'); ?></th>
            <th><?php echo $this->Paginator->sort('ABN'); ?></th>
            <th><?php echo "Billing Address"; ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?></th>
            <th><?php echo "Shipping Address"; ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?>
            <?php echo $this->Paginator->sort(''); ?></th>
            <th><?php echo $this->Paginator->sort('Phone'); ?></th>
            <th><?php echo $this->Paginator->sort('Email'); ?></th>
            <th><?php echo $this->Paginator->sort('Fax'); ?></th>
            <th><?php echo $this->Paginator->sort('Website'); ?></th>
            <th><?php echo $this->Paginator->sort('Description'); ?></th>
            <th><?php echo $this->Paginator->sort('License Number'); ?></th>
            <th class="actions"><?php echo __(''); ?></th>
    </tr>
    <?php foreach ($companies as $company): ?>

    <tr>
        <td><?php echo h($company['Company']['id']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_name']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['ABN']); ?>&nbsp;</td>
        <td><?php echo h($company['CompaniesBillingAddress']['company_street_address']); ?>&nbsp;
        <?php echo h($company['CompaniesBillingAddress']['company_suburb']); ?>&nbsp;
      <?php echo h($company['CompaniesBillingAddress']['company_state']); ?>&nbsp;
        <?php echo h($company['CompaniesBillingAddress']['company_postcode']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_street_address']); ?>&nbsp;
        <?php echo h($company['Company']['company_suburb']); ?>&nbsp;
        <?php echo h($company['Company']['company_state']); ?>&nbsp;
        <?php echo h($company['Company']['company_postcode']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_phone']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_email']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_fax']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_website']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_description']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['license_number']); ?>&nbsp;</td>
        <td class="actions">

            <?php echo $this->Html->link(__('View'), array('action' => 'view', $company['Company']['id'])); ?>
            </td>
    </tr>
<?php endforeach; ?>
    </table>

companiesController:

公共数组= $components (‘分页器’);

代码语言:javascript
运行
复制
public function index() {
    $this->Company->recursive = 0;
    $this->set('companies', $this->Paginator->paginate());
}

//一些代码}

companiesBillingAddressController

代码语言:javascript
运行
复制
public $components = array('Paginator');

    public function index() {
        $this->CompaniesBillingAddress->recursive = 0;
        $this->set('companiesBillingAddresses', $this->Paginator->paginate());
    }}

CompaniesBillingAddress表属于公司表。我希望companiesBillingAddress表中的数据显示在companies/index.ctp中。我一直收到一个错误,说“未定义的索引: CompaniesBillingAddress [APP\View\Companies\index.ctp,第36行”。有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2014-05-09 16:57:52

代码语言:javascript
运行
复制
$this->Company->recursive = 1;

在您的companiesController中应该可以完成这项工作。在像“未定义索引”这样的情况下,在视图中调试数组是一种很好的方法。如果你没有找到你想要的数据,看看你的控制器。在这种情况下,错误是您禁用的递归。

此外,我不建议使用递归,而是使用containable behaviour。因此,将递归设置为0实际上是一个很好的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23557866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档