【发布时间】:2017-01-06 15:13:10
【问题描述】:
我创建了一个表单,我需要在其中显示一个表,该表的数据来自两个不同的表。数据来自分支表。这个 Branch 表包含一个外键 PrimaryContactID(int),它将包含 Employee 表的主键。
现在,在索引页面中,我显示了 Branch 表中的所有详细信息,但 PrimaryContactID 除外。而不是我需要显示该表中的名称。我就是这样尝试的:
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 表中获取名称,而不是这样。我不知道该怎么做。有人可以说我应该改变什么,这样我才能得到这个名字吗?
【问题讨论】:
-
你需要给出这两个表之间的关系。
-
@PruthvirajChudasama:这就是我不知道如何给予的。我只是要求这个。
标签: php cakephp cakephp-3.2