CakePHP 3.6.11:联接表的where子句

时间:2018-09-27 10:47:25

标签: php cakephp foreign-keys cakephp-3.6

我有以下3张桌子:

客户:

customerstable

服务:

servicestable

客户服务:

customerservicestable

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的{​​{1}}函数中,添加了以下几行:

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来实现此表的添加,编辑功能吗?

编辑

在ndm建议之后,我将其更改为:

CustomerservicesController.php

但是它不起作用。 //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']]); }); 可能无法正常工作,因为如果我将其替换为1(“客户ID”),则其正常工作。知道为什么不起作用吗?

2 个答案:

答案 0 :(得分:0)

我通过添加use ($id)$id而不是$this->data['Customers']['id']来解决此问题:

//Get customerservices for specific customer
        $servicesTable = TableRegistry::get('Services');
        $services = $servicesTable->find()->matching('Customerservices', function(\Cake\ORM\Query $q) use ($id) {
                return $q->where(['Customerservices.customerid' => $id]);
            });

答案 1 :(得分:0)

尝试以下方法:

$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']是否包含您的期望,请看一下其中的内容:

debug($this->data['Customers']);

// or
debug($this->data);

// or
print_r($this->data['Customers']);

如果这是请求对象发布的数据,请在此处查看: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');
相关问题