I have an edit.ctp page where I have a form to edit the selected Workflow. In this form I’m creating a multiselect element where the user can select multiple WorkflowObjects:
Why creating a save controller while the framework provides a save Table::save() function and besides the data manipulating functions are better declared in the Model class because that is where data logic are done.
From what you are talking about your edit action should be like this :
public Function edit($id) { $workflow = $this->workflows->get($id,['contain'=>['WorkflowObjects]]); if ($this->request->is('post')) { $this->Workflows->patchEntity($workflow, $this->request->data, ['associated' => ['WorkflowObjects']]); if ($this->Workflows->save($workflow)) { $this->Flash->success(__('Work Object where successfully saved')) } $this->redirect(['action' => 'index', $workflowId]); } }
Thanks for the reply but it doesn’t really address the issue. The code you’ve put in the edit function is the same as my save function. I agree putting it in the edit function is cleaner but in terms of what the save is doing, it doesn’t matter which function it’s in, the result is always the same.
In a hasMany/belongsTo relationship if I’m passing it an array of ‘_ids’ for an associated table, it always creates new entitys instead of seeing if that id already exists and retrieving/updating it.
If that’s the intended behavior then I’ll have to resort to a work around.
I will advise you and for others people face to same problem :
When you edit entity with association and you want upsert association and combine delete other entities not used you must prepare response such as :
Provide id or primary key in parameters for all entities you would only update value