使用cakephp在index()中显示来自两个不同表的数据。 [英] Display data from two different tables in the index() using cakephp.

查看:172
本文介绍了使用cakephp在index()中显示来自两个不同表的数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表单,我需要显示一个表,其中的数据来自两个不同的表。数据来自分支表。这个分支表包含一个外键PrimaryContactID(int),它将包含Employee表的主键。

I've created a form where I need to display a table for which the data is coming from two different tables. The data is coming from Branch table. This Branch table contains a foreign key PrimaryContactID(int) which will contain the primary key of Employee table.

现在,在索引页中,我显示了所有的细节分支表,除了PrimaryContactID。而不是我需要显示从该表的名称。这是我的尝试:

Now, in the index page I have display all the details from the Branch table, except for the PrimaryContactID. Instead of that I need to display the names from that table. This is how I tried:

controller:
public function index()
    {
        $branch = $this->paginate($this->Branch);

        $this->set(compact('branch'));
        $this->set('_serialize', ['branch']);
    }
index.ctp:
<div class="branch index large-9 medium-8 columns content">
    <h3><?= __('Branch') ?></h3>
    <table cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th><?= $this->Paginator->sort('BranchID') ?></th>
                <th><?= $this->Paginator->sort('BranchName') ?></th>
                <th><?= $this->Paginator->sort('BranchCode') ?></th>
                <th><?= $this->Paginator->sort('Telephone') ?></th>
                <th><?= $this->Paginator->sort('PrimaryContactID') ?></th>
                <th class="actions"><?= __('Actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($branch as $branch): ?>
            <tr>
                <td><?= $this->Number->format($branch->BranchID) ?></td>
                <td><?= h($branch->BranchName) ?></td>
                <td><?= h($branch->BranchCode) ?></td>
                <td><?= h($branch->Telephone) ?></td>
                <td><?= $this->Number->format($branch->PrimaryContactID) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['action' => 'view', $branch->BranchID]) ?>
                    <?= $this->Html->link(__('Edit'), ['action' => 'edit', $branch->BranchID]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $branch->BranchID], ['confirm' => __('Are you sure you want to delete # {0}?', $branch->BranchID)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->prev('< ' . __('previous')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
        </ul>
        <p><?= $this->Paginator->counter() ?></p>
    </div>
</div>



通过使用上述方法,我可以检索PrimaryContactID(int)。但是,而不是我需要从Employee表中获取名称。我不知道该怎么做。可以有人说我应该改变什么,这样我会得到名字?

By using the above method, I could retrieve the PrimaryContactID(int). But, instead of that I need to get the name from the Employee table. I don't know how to do that. Can some one say what should I change, so that I would get the name?

推荐答案

$this->paginate = [
    'contain' => ['Employee'] // Employees?
];
$branch = $this->paginate($this->Branch);

并在您的视图中:

<?= h($branch->employee->name) ?>

这篇关于使用cakephp在index()中显示来自两个不同表的数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆