Re: SaveAll + one-to-many

2009-01-26 Thread Jelmer

Hi Joshua,

If you have a $data array containing data for a record in A and 0 or
more records in B, you can use saveAll.

So, if your $data is something like:

array(
  [A] = array(
[id] = 12,
[otherdata] = 'some string'
),

 [B] = array(
[0] = array(..),
[1] = array(..)
 )
);

you can call model A in your a-controller:

A-saveAll($data,array('atomic' = true, 'validate' = 'first'));

This will save all the data in the A-record and associated B-records.
But take care:

- This will only work if you have set up the relationship. So in A
there must be something like:
 var $hasMany = array('B' = array('dependent' = true))
  and in B:
 var $belongsTo = 'A'

  Check the meaning of these options here:
http://book.cakephp.org/view/78/Associations-Linking-Models-Together

- The 'atomic' option only works if you can use transactions, like in
mysql InnoDB (not the default myIsam) tables.

- Tip: put this before the line where you want to use saveAll, to see
if the $data is ok:
 pr($data); exit();


This will save A + B. The only thing it will not do is remove the
records in B that no longer belong to A. This is something I will post
an item about, right now.

Jelmer

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



SaveAll + one-to-many

2009-01-12 Thread Joshua

I have no idea on how to save model A that has a one-to-many
relationship with module B.
Example:
---
table A
id
name
--
table B
id
a_id
name
--
How to do this if I want to save A with many B records in model A
function.

I really google a lot of resource, but I didn't find the answer there.
http://www.packtpub.com/article/working-with-simple-associations-using-cakephp
http://blog.matsimitsu.nl/code/166/saveall-with-cakephp-part2

I will appreciate for your answer.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---