0

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

netdev
  • 496
  • 1
  • 5
  • 22
  • Do the blog tutorial in the official documentation. You obviously have not read about the very most basic tasks like passing a variable to the view layer. Your approach by getting spoon-fed by other people is flawed, start structured learning by reading the documentation. – floriank Sep 26 '18 at 12:24
  • @burzum thank you for your recommendation but I still can't find in the tutorial how to pass a `Services` list from `CustomerController` to the View. I would be glad if you can provide an example – netdev Sep 26 '18 at 13:00
  • @burzum please check the updated post. – netdev Sep 26 '18 at 13:31
  • I think the larger problem here is that there is in fact a relationship between customers and services based on your post. A customer can have many services and a service can belong to many customers. You should refactor your Cake tables to reflect this relationship and access the required data accordingly. – Derek Fulginiti Sep 26 '18 at 16:41
  • @DerekFulginiti the fact is that I didn't want to make a direct relationship because a customer can be without any services and a service can be without any customer, thats why I am using the customerServices table... But ofcourse as a newbie I don't know if that is the best way – netdev Sep 27 '18 at 06:08
  • That's fine. You should still set up the many-many relationship between the two tables through your customerServices join table – Derek Fulginiti Sep 27 '18 at 09:44
  • @netdev https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/articles-controller.html The page that explains MVC mentions the concept as well, the view chapter as well. Read the documentation. – floriank Sep 27 '18 at 12:07

1 Answers1

0

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

TableRegistry::getTableLocator()->get('Services')->find(...)
Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35