开发者

saveall not saving associated data

开发者 https://www.devze.com 2023-01-02 06:50 出处:网络
I\'m having a problem trying to save (update) some associated data. I\'ve read about a million google returns, but nothing seems to be the solution. I\'m at my wit\'s end and hope some kind soul here

I'm having a problem trying to save (update) some associated data. I've read about a million google returns, but nothing seems to be the solution. I'm at my wit's end and hope some kind soul here can help.

I'm using 1.3.0-RC4, my database is in InnoDB.

Course has many course_tees

CourseTee belongs to course

My controller function is pretty simple (I've made it as simple as possible):

if(!empty($this->data))
$this->Course->saveAll($this->data);

I've tried a lot of d开发者_如何学Pythonifferent variations of that $this->data['Course'], save($this->data), etc without luck.

It saves the Course info, but not the CourseTee stuff. I don't get an error message.

Since I don't know how many tees any given course will have, I generate the form inputs dynamically in a loop.

$form->input('CourseTee.'.$i.'.teeName', array(
    'error' => false,
    'label' => false,
    'value'=>$data['course_tees'][$i]['teeName']
))

The course inputs are simpler:

$form->input('Course.hcp'.$j, array(
    'error' => false,
    'label' => false,
    'class' => 'form_small_w',
    'value'=>$data['Course']['hcp'.$j]
))

And this is how my data is formatted:

Array
(
  [Course] => Array
  (
    [id] => 1028476
    ...
  )

  [CourseTee] => Array
  (
    [0] => Array
  (
    [key] => 636
    [courseid] => 1028476
    ...
  )

  [1] => Array
  (
    [key] => 637
    [courseid] => 1028476
    ...
  )

  ...

  )
)


According to CakePHP conventions you must provide [course_id] => 1028476 and not [courseid] => 1028476. Check you model bindings as well (capitalizations and underscores). There must be "Course has many CourseTee". Do this way to save:

if ($this->Course->saveAll($this->data, array('validate' => 'first'))) {
  $this->_flash(__('Successfully saved.', true), 'success');
} else {
  $this->_flash(__('Cannot save. Does not validates.', true), 'error');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消