0

我有这 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)替换它,它会按预期工作。知道为什么不工作吗?

4

2 回答 2

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]);
            });
于 2018-09-27T13:36:42.450 回答
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');
于 2018-09-27T13:37:48.707 回答