CakePHP 3.6.11:连接表的 Where 子句

CakePHP 3.6.11: Where clause of joined tables

我有这 3 个 table:

客户:

customerstable

服务:

servicestable

客户服务:

customerservicestable

CustomerservicesTable.php中的关系:

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

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

Customers 编辑页面我想添加一个 table 与特定客户的 Services (然后添加新的,编辑现有的等)。

所以在 Template\Customers\edit.ctp 我有这个 table:

<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>

并且在 Controller\CustomersController.phpedit 函数中我添加了这些行:

//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来实现这个table的添加、编辑功能吗?

编辑

根据 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),它会按预期工作。知道为什么不起作用吗?

我通过添加 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]);
            });

试试这个:

$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');