1

I have a many-to-many relation between 2 models Invoiceand Position defined with the hasMany through method like described in the Cookbook.

Now when i create a Invoice, i want to save multiple Positions to that Invoice in one save()-operation. Something like this:

$data = array(
  'Invoice' => array(
    // invoice stuff 
  ),
  'InvoicesPosition' => array(
    [0] => array(
      // additional meta information
      'Position' => array(
        // position stuff
        // ...
      ),
    ),
    [1] => array(
      // ...
      'Position' => array(
        // position stuff
        // ...
      ),
    ),
    // and so on
  ),
);  

$this->Invoice->saveAll($data, array('deep' => true));

The result should be one new Invoice, with 2 Positions linked to it (That means 1 new record in the invoices table, 2 new records in the positions table and 2 new records in the join table).

Is it possible with Cake's built-in methods? Or do i have to overwrite the saveAll()-method for that model?

2
  • its a bit odd to fix the wrong code in the question, now it looks like it was correct in the first place, beter keep original question, and the approved answers will show other users with same issue what to change
    – Ceeram
    Aug 23, 2012 at 15:20
  • Will keep that in mind for the next time. Sorry.
    – rookian
    Aug 24, 2012 at 16:21

1 Answer 1

1

Use 'deep' => true option in saveAll() method.

$this->Invoice->saveAll($data, array('deep' => true));
1
  • Ah nice, thanks. I had to edit my data Array. See my code above.
    – rookian
    Aug 23, 2012 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.