Search code examples
cakephp-3.0has-manycakephp-model

cakephp 3.x data not saving when Saving data into 2 tables with has many relation


cakephp 3.x data not saving when Saving data into 2 tables with has many relation

I have two tables articles and comments, comments has field article_id relation between them us article hasMany Comments and Comment belongs to Article, i have following code in my article/add.ctp view file

<?php
    $this->Form->create($article)
    echo $this->Form->input('title');
    echo $this->Form->input('status');
    echo $this->Form->input('comments.0.title');
    echo $this->Form->input('comments.0.description');
    $this->Form->end()
?>

This is my controller code

public function add() {

    $article = $this->Articles->newEntity();

    if ($this->request->is('post')) {
        $article =  $this->Articles->patchEntity($article, $this->request->data, [
                        'associated' => ['Commments']
                    ]);
        if ($this->Articles->save($article)) {
            $this->Flash->success(__('The article has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The article could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('article'));
    $this->set('_serialize', ['article']);
}

This is my Article model

<?php
    class ArticlesTable extends Table {
        public function initialize(array $config) {
            parent::initialize($config);
            $this->table('articles');
            $this->displayField('title');
            $this->primaryKey('id');
            $this->addBehavior('Timestamp');
            $this->hasMany('Comments', [
                'foreignKey' => 'article_id'
            ]);
        }
    }

?>

This is my Comment model

<?php
    class CommentsTable extends Table {
        public function initialize(array $config) {

            parent::initialize($config);
            $this->table('comments');
            $this->displayField('id');
            $this->primaryKey('id');

            $this->addBehavior('Timestamp');

            $this->belongsTo('Articles', [
                'foreignKey' => 'article_id',
                'joinType' => 'INNER'
            ]);
        }
    }
?>

and when i print the $article after patch entity than showing below array its not createing the comment model object its create simple array under the article model object

App\Model\Entity\Article Object
(
    [title] => Article1
    [status] => 1
    [comments] => Array
    (
        [0] => Array
            (
                [title] => Article 1 Comment 1
                [description] => mnftrduio
            )

    )

[[new]] => 1
[[accessible]] => Array
    (
        [*] => 1
    )

[[dirty]] => Array
    (
        [title] => 1
        [status] => 1
        [comments] => 1
    )

But data save only in article table not in comment table


Solution

  • nothing wrong with your code, just missing some echo, semicolon, and mistype comments with triple 'm'

    $this->Form->create($article) //missing echo and semicolon;
    echo $this->Form->create($article); //the correct one
    
    
    $this->Form->end() //missing submit button
    
    echo $this->Form->button(__('Submit')); //the correct one
    echo $this->Form->end(); //the correct one
    
    
    $article =  $this->Articles->patchEntity($article, $this->request->data, [
                        'associated' => ['Commments']
                    ]); //mistype comments with triple "m" 
    
    $article =  $this->Articles->patchEntity($article, $this->request->data, [
                        'associated' => ['Comments']
                    ]);// the correct one
    

    after you correct it, it should working :)