php - 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>
但它总是显示空白。我怎样才能改变它才能工作?
解决方案
更改视图功能如下:
$customer = $this->Customers->get($id, [
'contain' => ['CustomerServiceTypes' => ['ServiceTypes']]
]);
$this->set('customer', $customer);
在 view.ctp 中:
<td><?= h($customerServiceTypes->service_type->title) ?></td>
推荐阅读
- swift - 即使在保护自我和非可选属性之后,也可以获取“在初始化之前使用的变量‘self.xxx’”
- ios - 如何处理拍摄的图像,然后使用 Swift 将结果查看到“文本视图”?
- mongodb - MongoDB 从子子数组中删除所有匹配项
- java - 来自先前 Spring Boot 测试的 Quartz 作业随机仍在运行
- javascript - Libgdx HTML5 AnimationScheduler 长度为空
- python - Flask+Celery 作为守护进程
- excel - 在加载项中将宏添加到 Excel 功能区
- java - Maven 无法下载 geotools 依赖项
- r - 更改数据集时,在闪亮的应用程序中动态显示列名闪烁错误
- ruby-on-rails - 处理可能更改的 json 响应(并非每次都包含所有键,可以添加新键)