Search code examples
phpcakephpcomboboxdropdowncakephp-3.6

CakePHP 3.6.11: create dropdown in ctp from another controller


I have customers(id, name) and services (id, title, isSubscription), I want to add a dropdown list in the add.ctp of Customers which will contain all the titles of Services where isSubscription is true. How I can achieve that?

There is no relation between Customers and Services, it will just populate the customerServices (customerid, serviceid) table with id of the specific customer and the id of the selected service.

Here is what I have tried:

in ServicesController.php in add function:

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

in add.ctp in Template/Customers :

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

But I get this :

Call to a member function find() on boolean


Solution

  • The documentation includes a section entitled Getting Instances of a Table Class, which shows exactly what you need:

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