tags:

views:

29

answers:

1

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 different 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
    ...
  )

  ...

  )
)
A: 

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');
}
bancer
Firstly, thanks deceze for formatting my post properly. I'll try to remember to do that next time.Bancer, that's brilliant. It's working. I had changed my model bindings to what you suggested, but since it was also wrong in my views, my tees didn't show and I assumed it was incorrect. I'm not sure why I didn't see it since all the other models were different.Thanks again for your time.
junior29101