【问题标题】:Display data from two different tables in the index() using cakephp.使用 cakephp 在 index() 中显示来自两个不同表的数据。
【发布时间】: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


【解决方案1】:

很简单,添加控制器:

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

在你看来:

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

【讨论】:

  • 我收到此错误:尝试获取非对象的属性。我想我也需要在 index() 的某个地方给出关系。但是,我不知道在哪里做。你能在上面的代码中检查一下吗?
  • 是的,您需要在分页选项中添加 conatin。我将添加到我的答案中
  • 那我该怎么办:$branch = $this->paginate($this->Branch);这也应该存在吗?
【解决方案2】:
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'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多