我有这张桌子:
客户[id,姓名,姓氏,电话,文字,余额,已创建]
service_types [id,标题,价格,长度,is_subscription,已创建]
customer_service_types [id,customer_id,service_type_id,价格,已创建]
在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>
但是它始终显示为空白。我该如何更改才能正常工作?
答案 0 :(得分:1)
更改视图功能如下:
$customer = $this->Customers->get($id, [
'contain' => ['CustomerServiceTypes' => ['ServiceTypes']]
]);
$this->set('customer', $customer);
在view.ctp中:
<td><?= h($customerServiceTypes->service_type->title) ?></td>