首页 > 解决方案 > CakePHP 3.6.11:从另一个控制器在 ctp 中创建下拉菜单

问题描述

我有customers(id, name) 和services(id, title, isSubscription),我想在其中添加一个下拉列表add.ctpCustomers其中将包含所有titlesServiceswhere isSubscriptionis true。我怎样才能做到这一点?

Customers和之间没有关系Services,它只会用特定客户和所选服务的customerServices(customerid, serviceid) 表填充。idid

这是我尝试过的:

ServicesController.phpadd function

$services = $this->Services->find('list'); //the error is here because there is no relationship between Customers and Services
$this->set(compact('services'));

add.ctpTemplate/Customers

$this->Form->control('category',array('options' => $services));

但我明白了:

在布尔值上调用成员函数 find()

标签: phpcakephpcomboboxdropdowncakephp-3.6

解决方案


该文档包含一个标题为“获取表类实例”的部分,它准确地显示了您需要的内容:

TableRegistry::getTableLocator()->get('Services')->find(...)

推荐阅读