php - Save data to two different tables in CakePHP 3
Get the solution ↓↓↓Solution:
Try and Error leaded me to the solution + Reading the Docs again... and again.
Articles Controller
public function add()
{
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => [
'Contents'
]
]);
// Added this line
$article->user_id = $this->Auth->user('id');
if ($this->Articles->save($article, array('deep' => true))) {
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('article', $article);
}
Test add.ctp in Articles
echo $this->Form->hidden('contents.0.article_id');
echo $this->Form->hidden('contents.0.type', ['value' => '1']);
echo $this->Form->hidden('contents.0.position', ['value' => '3']);
echo $this->Form->hidden('contents.0.text', ['value' => 'test']);
echo $this->Form->hidden('contents.1.article_id');
echo $this->Form->hidden('contents.1.type', ['value' => '7']);
echo $this->Form->hidden('contents.1.position', ['value' => '7']);
echo $this->Form->hidden('contents.1.text', ['value' => 'test7']);
And added this to my ArticlesTable.php
$this->hasMany('Contents');
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.