我有这 3 张桌子:
顾客:
服务:
客户服务:
有了这个关系CustomerservicesTable.php
:
$this->belongsTo('Customers')
->setForeignKey('customerid');
$this->belongsTo('Services')
->setForeignKey('serviceid');
在Customers
编辑页面中,我想添加一个包含Services
特定客户的表格(然后添加新的、编辑现有的等)。
所以在Template\Customers\edit.ctp
我有这张桌子:
<h3><?= __('Services') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('Date') ?></th>
<th scope="col"><?= $this->Paginator->sort('Service') ?></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->created) ?></td>
<td><?= h($service->title) ?></td>
<td><?= $this->Number->format($service->price) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $customer->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $customer->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $customer->id], ['confirm' => __('Are you sure you want to delete # {0}?', $customer->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
并在我的edit
功能中Controller\CustomersController.php
添加了以下几行:
//Get customerservices for specific customer
$servicesTable = TableRegistry::get('Services');
$services = $servicesTable->find('all');//->where(['Services.Customerservices.id =' => $this->data['Customers']['id']]);
$this->set(compact('services'));
我已经评论了这where
部分。我如何更改它以仅获得属于特定客户的服务?使用customerservicesTable
?
之后我可以直接编辑CustomerservicesController.php
来实现这个表的添加、编辑功能吗?
编辑
在 ndm 建议后,我将其更改为:
//Get customerservices for specific customer
$servicesTable = TableRegistry::get('Services');
$services = $servicesTable->find()->matching('Customerservices', function(\Cake\ORM\Query $q) {
return $q->where(['Customerservices.customerid' => $this->data['Customers']['id']]);
});
但它不起作用。可能$this->data['Customers']['id']
不起作用,因为如果我用 1(客户 ID)替换它,它会按预期工作。知道为什么不工作吗?