首页 > 解决方案 > CakePHP 3.6.11:视图中连接表的访问属性

问题描述

我有这个表:

客户[id, name, surname, phone, text, balance, created]

service_types[id, title, price, length, is_subscription, created]

customer_service_types[id, customer_id, service_type_id, price, created]

view.ctp我想查看Services分配给当前客户的所有内容。

Bake创造了view function这个CustomersController.php

public function view($id = null)
    {
        $customer = $this->Customers->get($id, [
            'contain' => ['CustomerServiceTypes']
        ]);

        $this->set('customer', $customer);
    }

view.ctp我想显示service_type -> title所以这里是我尝试这样做的方式:

<td><?=$customerServiceTypes->service!==null ? h($customerServiceTypes->service->title) : '' ?></td>

但它总是显示空白。我怎样才能改变它才能工作?

标签: phpjoincakephpmodel-view-controllerassociations

解决方案


更改视图功能如下:

$customer = $this->Customers->get($id, [
            'contain' => ['CustomerServiceTypes' => ['ServiceTypes']]
        ]);


        $this->set('customer', $customer);

在 view.ctp 中:

<td><?= h($customerServiceTypes->service_type->title) ?></td>

推荐阅读