CakePHP 3.6.11:视图中联接表的访问属性

时间:2018-10-05 08:38:28

标签: php join cakephp model-view-controller associations

我有这张桌子:

  

客户[id,姓名,姓氏,电话,文字,余额,已创建]

     

service_types [id,标题,价格,长度,is_subscription,已创建]

     

customer_service_types [id,customer_id,service_type_id,价格,已创建]

view.ctp中,我要查看分配给当前客户的所有Services

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

但是它始终显示为空白。我该如何更改才能正常工作?

1 个答案:

答案 0 :(得分:1)

更改视图功能如下:

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


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

在view.ctp中:

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