4

I'm trying to save data with following structure: alt text

As you can see, there is HABTM association between users and experiences table. And another HABTM between experiences_users and tags. I created following form:

<?php echo $form->create('Experience', array('action' => 'addClassic'));?>
    <?php
    echo $form->input('Experience.date', array('dateFormat' => 'DMY'));
    echo $form->input('Experience.time', array('timeFormat' => '24', 'empty' => array(-1 => '---'), 'default' => '-1'));
    echo $form->input('Experience.name');
    echo $form->input('ExperiencesUser.1.note');
    echo $form->input('ExperiencesUser.1.rating'); 
    //echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
    //echo $form->input('ExperiencesUser.1.Tags', array('multiple' => 'multiple', 'options' => $tags));
    //echo $form->input('ExperiencesUser.1.Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
    echo $form->input('ExperiencesUser.1.confirmed', array('type' => 'hidden', 'value' => '1'));

    echo $form->input('ExperiencesUser.1.user_id', array('type' => 'hidden'));
    echo $form->input('ExperiencesUser.2.user_id', array('type' => 'hidden'));
    ?>
<?php echo $form->end(__('Add', true));?>

And everything works well. saveAll method creates new Experience, assings this new experience to two users (via experiences_users) and sets the stuff around.

What bothers me is that I want to assign some Tags to newly created experiences_users record (to the first one). I thought, that should be done via some of the commented stuff. Every line of this commented code creates form and sends data to $this->data, but it never gets saved.

So my question is: What is the right syntax of $this->data or $form->input in my example to save data into experiences_users_tags?

Spooky
  • 2,966
  • 8
  • 27
  • 41
picca
  • 857
  • 1
  • 10
  • 16

1 Answers1

5

I figured it out. Cake saveAll function works only for directly associated models. Luckily Cake is able to take care of this:

view

echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));

controller

$this->Experience->saveAll($data, $parameters);
$this->Experience->ExperiencesUser->save($data);

Cake after calling saveAll() fills $this->data with last inserted id. So second call save() will save data to the right table and set properly foreign keys.

picca
  • 857
  • 1
  • 10
  • 16