In there manual there is an example of saving associated data.

If I recall correctly, there are two saves needed though.

http://tempdocs.cakephp.org/#TOC89634

This gives you exactly what you need.

If you REALLY need one save, then you need to write your own function.

On Dec 20, 2007 4:17 PM, francky06l < [EMAIL PROTECTED]> wrote:

>
> As far as I know, save (opposite to "read" is for a single
> model) ..seems you have to implement multiple save
>
>
> On Dec 20, 9:57 pm, Cake Fan <[EMAIL PROTECTED]> wrote:
> > I'm having a relatively simple problem (at least I hope).  I'm hoping
> > I'm simply not understanding the framework properly, but perhaps
> > someone can help me solve my confusion.
> >
> > The problem I'm having is related to saving data to multiple models in
> > one save.  Perhaps this idea is voodoo, but in the below code it would
> > be highly advantageous to be able save data to both the ZipCode and
> > the Business model in the same call to save().  I was hoping it would
> > be straightforward with cake 1.2 out of the box by simply setting up
> > the hasOne and belongsTo relationship in the models.
> >
> > Right, now the Business Name seems to work just fine.  However, the
> > Zip Code doesn't save.  See the code below for what I've done, I'll
> > discuss what I've tried below the code snippets.
> >
> > [In the live code, there are a lot more  (multiple more hasOne and
> > hasMany) relationships than shown below, but I removed many of them
> > for clarity]
> > ====================================
> > ::::::::::::::::BUSINESS MODEL:::::::::::::::::::::::::::
> > ------------------------------------------------------------------------
> > class Business extends AppModel
> > {
> >     var $useTable = 'businesses';
> >
> >     var $validate = array(
> >         'name' => array(
> >             'minlength' => array(
> >                 'rule' => array('minLength', '1'),
> >                      'message' => 'A Business Name Is Needed!'
> >              )
> >         ),
> >        'zip_code' => array(
> >              'postal' => array(
> >                 'rule' => array('postal', null, 'us'),
> >                      'message' => 'Only valid US Zip Codes Allowed!'
> >              )
> >           )
> >        );
> >
> >     var $hasOne = array(
> >         'ZipCode' => array(
> >             'className'    => 'ZipCode',
> >             'dependent'    => true
> >         ));
> >
> > }
> >
> > ------------------------------------------------------------------------
> >
> > ====================================
> > ::::::::::::::::ZipCode MODEL::::::::::::::::::::::::::::::::
> > ------------------------------------------------------------------------
> > class ZipCode extends AppModel
> > {
> >     var $useTable = 'zip_codes';
> >
> >     var $belongsTo = array(
> >         'Business' => array(
> >             'className'    => 'Business',
> >             'foreignKey'    => 'business_id'
> >         )
> >     );}
> >
> > ------------------------------------------------------------------------
> >
> > ====================================
> > ::::::::::::::::Add A BUSINESS VIEW::::::::::::::::::::
> > ------------------------------------------------------------------------
>
> > <h2>Add A Business</h2>
> > <?php echo $form->create('Business', array('type' => 'post', 'action'
> > => 'add', 'id' => 'request-form')); ?>
> >    <?php echo $form->label('Business.name', 'Business name'); ?>
> >    <?php echo $form->input(' Business.name', array('type' => 'text',
> > 'label' => false)); ?>
> >    <?php echo $form->label('Business.zip_code', 'Zip Code'); ?>
> >    <?php echo $form->input(' Business.zip_code', array('type' =>
> > 'text', 'label' => false)); ?>
> > <?php echo $form->end('Add It!'); ?>
> > ------------------------------------------------------------------------
>
> >
> > ====================================
> > ::::::::::::::::ADMIN CONTROLLER:::::::::::::::::::::::
> > ------------------------------------------------------------------------
> > class AdminController extends AppController {
> >     var $helpers = array('html', 'time', 'javascript', 'ajax');
> >     var $uses = array('Business','ZipCode');
> >
> >     /** main action (starting point) **/
> >     function index ()
> >     {
> >         $this->pageTitle = 'The Back End';
> >
> >     }
> >
> >     /** main action (starting point) **/
> >     function add ()
> >     {
> >         $this->pageTitle = 'Add A Business';
> >
> >         /** we have a search **/
> >         if (!empty($this->data)) {
> >
> >            //If the form data can be validated and saved...
> >            if($this->Business->save($this->data, true,
> > array('name','zip_code'))){
> >
> >                //Set a session flash message and redirect.
> >                $this->Session->setFlash('Business Added!');
> >                $this->redirect('/admin/index');
> >                exit(0);
> >            }
> >         }
> >     }
> >
> > }
> >
> > ------------------------------------------------------------------------
>
> >
> > To get it working, I've tried the following with no successful saves
> > of the ZipCode field:
> >
> > 1) Changing the form Class name:
> > <?php echo $form->input('ZipCode.zip_code ', array('type' => 'text',
> > 'label' => false)); ?>
> >
> > 2) Changing the controller save call around in various ways:
> > if($this->Business->save($this->data, true, array('name','zip_code')))
> >  if($this->Business->save($this->data['Business'])) ...
> >
> > Essentially, I would like the data to be saved to both models in one
> > call; I have whole lot of models in this particular system, and
> > calling a bunch of saves to all the individual models isn't really
> > practical.
> >
> > Thanks for your help in advance!
> > CakeFan
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to