bikash.bilz
bikash.bilz

Reputation: 821

Saving / Creating data to associated table is not working

I have three table,

Authors

Stories

Details

Stories and Details are associated with the Authors, with hasOne relationship.

Bellow is the code in my AuthorsController

function add(){
   if($this->request->is('POST')){
     $author = $this->Authors->newEntity($this->request->data,
           ['associated' => ['Stories', 'Details'], 'validate' => false]
     );
     $this->Authors->save($author);
  }
}

This is only saving data in the Authors table, not in the other two table. Bellow is the data I have in $this->request->data

'Authors' => [
    'name' => 'Bikash'
    'Age' => '22'
    'stories' => [
        'name' => 'Life Without the L'
        'gener_id' => 5
    ]
    'details' => [
         'qualification' => 'XYZ'
         'biography'
     ]

  ];

What I am missing?

Upvotes: 0

Views: 59

Answers (1)

ndm
ndm

Reputation: 60503

You are using the wrong property names, stories and details won't work.

The default association property name for hasOne associations, is the underscored singular variant of the association alias, so for Stories that would story, and for Details it would be detail.

See also

Upvotes: 2

Related Questions