使用cakephp显示index()中两个不同表的数据。

时间:2016-08-30 11:01:08

标签: php cakephp cakephp-3.2

我创建了一个表单,我需要显示一个表,数据来自两个不同的表。数据来自Branch表。此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表中获取名称。我不知道该怎么做。有人可以说我应该改变什么,以便得到这个名字吗?

2 个答案:

答案 0 :(得分:1)

这很简单,加入控制器:

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

并在您看来:

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

答案 1 :(得分:-1)

In User.php Model first make association rule according to the requirement
eg

public $belongsTo = array(
        'Profile' => array(
            'className' => 'Profile',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ));

/// in UsersController.php
function index(){
$this->paginate = [
    'contain' => ['Profiles'] 
];
$users = $this->paginate($this->Users);
$this->set(compact('users));

//in Users/index.ctp

foreach($users as $user)
{
 echo $user['Profile']['your_field_name'];
}