CakePHP 3.6.11:从控制器传递联接表到视图

时间:2018-10-03 12:36:46

标签: php join cakephp model-view-controller

我有以下3张桌子:

客户:

enter image description here

服务:

enter image description here

客户服务:

enter image description here

CustomerservicesTable.php中具有这种关系:

$this->belongsTo('Customers')
            ->setForeignKey('customerid');

$this->belongsTo('Services')
            ->setForeignKey('serviceid');

Customers编辑页面中,我想添加一个联合表,其中包含特定客户的Servicescustomerservices表的创建日期。

所以在Template\Customers\edit.ctp中,我有此表:

    <h3><?= __('Transactions') ?></h3>
<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th scope="col"><?= $this->Paginator->sort('Date') ?></th>
            <th scope="col"><?= $this->Paginator->sort('Transaction') ?></th>
            <th scope="col"><?= $this->Paginator->sort('Price') ?></th>
            <th scope="col" class="actions"><?= __('Actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($services as $service): ?>
        <tr>
            <td><?= h($service->...................) ?></td>
            <td><?= h($service->title) ?></td>
            <td><?= $this->Number->format($service->price) ?></td>
            <td class="actions">
                <?= $this->Html->link(__('View'), ['controller'=>'customerservices','action' => 'view', $service->id]) ?>
                <?= $this->Html->link(__('Edit'), ['controller'=>'customerservices','action' => 'edit', $service->id]) ?>
                <?= $this->Form->postLink(__('Delete'), ['controller'=>'customerservices','action' => 'delete', $service->id], ['confirm' => __('Are you sure you want to delete # {0}?', $customer->id)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

,并且在edit的{​​{1}}函数中,添加了以下几行:

Controller\CustomersController.php

我如何修改它以显示customerservice->在日期字段中为特定客户创建的日期?

我在使用//Get customerservices for specific customer $servicesTable = TableRegistry::get('Services'); $services = $this->paginate($servicesTable->find()->matching('Customerservices', function(\Cake\ORM\Query $q) use ($id) { return $q->where(['Customerservices.customerid' => $id]); }), ['scope' =>'services']); $this->set(compact('services')); 函数之后添加了创建日期的列,并删除了bake的内容,是否足以访问此新列值?

0 个答案:

没有答案