CakePHP 3.6.11:联接表的where子句 [英] CakePHP 3.6.11: Where clause of joined tables
问题描述
我有以下3个表:
客户:
服务:
customerservices:
customerservices:
与此关系在 CustomerservicesTable.php
中:
$this->belongsTo('Customers')
->setForeignKey('customerid');
$this->belongsTo('Services')
->setForeignKey('serviceid');
在 Customers
编辑页面中包含特定客户服务
的表(然后添加新客户,编辑现有客户等)。
In Customers
edit page I want to add a table with the Services
of the specific customer (and then add new, edit existing etc).
因此, 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
函数中c $ c> Controller\CustomersController.php 我添加了以下几行:
and in the edit
function of Controller\CustomersController.php
I have added these lines:
//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
?
And I have commented the where
part. How I can change it in order to get only the services that belong to the specific customer? using the customerservicesTable
?
之后,我可以直接编辑 CustomerservicesController.php
来实现此表的添加,编辑功能?
And after that can I edit directly the CustomerservicesController.php
to implement the add,edit functions of this table?
EDIT
在ndm建议之后,我将其更改为:
After ndm suggestion I changed it like this:
//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),则其正常工作。知道为什么不起作用吗?
But its not working. Probably the $this->data['Customers']['id']
is not working because if I replace it with 1 (the Customer id) its working as expected. Any idea why is not working?
推荐答案
请尝试以下操作:
$servicesTable = TableRegistry::get('Services');
$customerId = $this->data['Customers']['id'];
// how about $this->request->getData('Customer.id') ?
// pass variable to function with `use`
$services = $servicesTable->find()->matching('Customerservices', function(\Cake\ORM\Query $q) use ($customerId) {
return $q->where(['Customerservices.customerid' => $customerId]);
});
如果不确定 $ this-> data ['Customers '] ['id']
包含您的期望,只是看看其中有什么:
If you are not sure that $this->data['Customers']['id']
contains what you expect, just have a look what’s in there:
debug($this->data['Customers']);
// or
debug($this->data);
// or
print_r($this->data['Customers']);
如果这是请求对象发布的数据,请在此处查看: https://book.cakephp.org/3.0/zh-CN/controllers/ request-response.html#request-body-data
If this is data published by the Request Object, have a look here: https://book.cakephp.org/3.0/en/controllers/request-response.html#request-body-data
// An input with a name attribute equal to 'MyModel[title]' is accessible at
$title = $this->request->getData('MyModel.title');
这篇关于CakePHP 3.6.11:联接表的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!