Display data from two different tables in the index() using cakephp.

倾然丶 夕夏残阳落幕 提交于 2019-12-11 13:25:35

问题


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.

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>

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?


回答1:


It' very simple, add in controller:

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

and in your view:

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



回答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'];
}


来源:https://stackoverflow.com/questions/39225722/display-data-from-two-different-tables-in-the-index-using-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!