Carlos Mauricio Samour wrote: > How do I save more than one ModelB at te same time? > >
Hi Carlos and everyone. I am totally enjoying baking with Cake and thought I could throw in my 2 cents and help out. And I hope to get an answer on some of the questions I have posted in the past :) So Carlos, I did this and I think it is helpful for others. I made an app_model.php file in the app directory. All data and methods here will be available to all Models. Here is my file: <?php /** * Application model for Cake. * * Add your application-wide methods in the class below, your models * will inherit them. * * @package cake * @subpackage cake.cake */ class AppModel extends Model { function batchSave($data=array()) { if ( ! is_array($data) ) { return FALSE ; } $saveFlag = TRUE ; foreach($data as $newRecord) { $this->id = NULL ; $saveFlag = ($this->save($newRecord)) ? $saveFlag : FALSE ; } return $saveFlag ; } } ?> So now every Model will have a batchSave() function. It accepts an array of new model data and goes through and creates a new record for each one. My $saveFlag error return is somewhat hokey because if any one of the new models is not created it will return FALSE. Another alternative is to exit on the first failure, modify appropriately for your needs. Have fun, huy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~----------~----~----~----~------~----~------~--~---