You are processing the $ data ['abstracts'] array incorrectly, as a result of which the association is not saved. $ Data ['abstracts'] is expected to be an array of abstracts. Your problem is here:
$data['abstracts']['user_id'] = $article->user_id; $data['abstracts']['approved'] = 0;
You can easily fix this by changing this to:
foreach($data['abstracts'] as $index => $abstract) { $abstract['user_id'] = $article->user_id; $abstract['approved'] = 0; $data['abstracts'][$index] = $abstract; }
This should correctly iterate over the array of abstract arrays, set user_id and the approved keys correctly, and then store it correctly.
CakePHP 3.x Association Conservation Documentation
EDIT: A very interesting problem. Try it without using patchEntity, and use newEntity instead:
public function add() { if ($this->request->is('post')) { $data = $this->request->data; // Post process abstracts objects foreach($data['abstracts'] as $index => $abstract) { $abstract['user_id'] = $article->user_id; $abstract['approved'] = 0; $data['abstracts'][$index] = $abstract; } // Build newEntity $article = $this->Articles->newEntity($data, [ 'associated' => ['Abstracts'] ]); // Save our entity with associations if ($this->Articles->save($article, [ 'validate' => false, 'associated' => ['Abstracts'] ])) { $this->Flash->success(__('Your article has been saved.')); return $this->redirect(['action' => 'index']); } // On save fail $this->Flash->error(__('Unable to add your article.')); $this->set('article', $article); } }
EDIT 2: Your problem looks exactly in your form helper. Your current auxiliary input of the form creates an array of $ data, which looks like this:
$data = [ 'abstracts' => [ 'body' => 'example text' ], 'category' => 'Science' ];
SHOULD look like this:
$data = [ 'abstracts' => [ ['body' => 'example text'], ['body' => 'Im your second abstract'], ['body' => 'Abstract three!'] ], 'category' => 'Science' ];
The problem is this:
abstracts.body
which should read like (in array notation):
// abstracts.0.body echo $this->Form->input('abstracts.0.body', [ 'label' => 'summary of article', 'maxlength' =>'440', 'rows' => '7' ]);
I believe that this should be the last problem you have encountered.