Hi I have a Ticket which contains several workers with different roles.
In my Tickets add method, I want to show some Form->control select boxes with these types. That’s why I put in the TicketsTable class the following lines:
but in my TicketsController in the add method, I do not have some collect statement for tickets, because I want to create a new one.
When I try to select the clients or workers it will ends up in an error:
Hi, sorry, it was my mistake. Postet the wrong error message:
Table class for alias Clients could not be found.
It seems to me, that relation between these tables (or one table) is not wokring proper.
And since I am adding a new ticket, I have no fetching of tickets, because I want to add a new one. But for this, I need the information from the other table.
Here is my add method in controller:
$ticket = $this->Tickets->newEmptyEntity();
$this->Authorization->authorize($ticket);
$user = $this->Authentication->getIdentity();
if ($this->request->is('post')) {
$ticket = $this->Tickets->patchEntity($ticket, $this->request->getData());
if ($this->Tickets->save($ticket)) {
$this->Flash->success(__('The ticket has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The ticket could not be saved. Please, try again.'));
}
$clients = $this->Tickets->Clients->find('list', limit: 200)->all();
$workers = $this->Tickets->Workers->find('list', limit:200)->all();
$this->set(compact('ticket', 'workers', 'clients'));
To use $this->Tickets->Clients from the TicketsController, you need what you have in the TicketsModel. No requirement to have the reverse association set up in the WorkersTable, though of course you can if it makes sense.
Try changing from hasOne to belongsTo? I think that would better reflect the expected relationship (the workers exist regardless of the tickets; the tickets don’t exist without the workers).
If that doesn’t resolve it, maybe a bit of debugging of things like debug($this->Tickets->Clients) and debug(\Cake\Orm\TableRegistry::getTableLocator()->get('Workers')) would shed some light.