I dont know if this helps, but for me works:

----------------------------------------------------------------------
In the controller:
----------------------------------------------------------------------

...

    var $uses = array('Benchlink', 'Country', 'Report', 'Lookup', 
'Rank', 'Piece', 'Comment');

    var $name = 'Benchlinks';

...

----------------------------------------------------------------------
Add action
----------------------------------------------------------------------

....

    if (!empty($this->data)) {

            $this->cleanUpFields();

            $this->Benchlink->create();

            if ($this->Benchlink->save($this->data)) {


                $lastInsertId = $this->Benchlink->getLastInsertId();

                $this->data['Report']['id'] = $lastInsertId;
                $this->Report->save($this->data);

                $this->data['Lookup']['id'] = $lastInsertId;
                $this->Lookup->save($this->data);

                $this->data['Rank']['id'] = $lastInsertId;
                $this->Rank->save($this->data);

                $this->data['Piece']['id'] = $lastInsertId;
                $this->Piece->save($this->data);

                $this->data['Comment']['id'] = $lastInsertId;
                $this->Comment->save($this->data);


                $this->Session->setFlash('The Benchlink has been saved');
                $this->redirect(array('action'=>'index'), null, true);



            }

...

----------------------------------------------------------------------
My model
----------------------------------------------------------------------

<?php


class Benchlink extends AppModel {

    var $name = 'Benchlink';

    var $order = 'Benchlink.id DESC';

    var $hasOne = array(

            'Report' => array('className' => 'Report',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'dependant' => 'true',
                                'counterCache' => ''),

            'Lookup' => array('className' => 'Lookup',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'dependant' => 'true',
                                'counterCache' => ''),

            'Rank' => array('className' => 'Rank',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'dependant' => 'true',
                                'counterCache' => ''),

            'Piece' => array('className' => 'Piece',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'dependant' => 'true',
                                'counterCache' => ''),

            'Comment' => array('className' => 'Comment',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'dependant' => 'true',
                                'counterCache' => '')

    );

    var $belongsTo = array(

            'Country' => array('className' => 'Country',
                                'foreignKey' => '',
                                'conditions' => 'Benchlink.country_id = 
Country.id',
                                'fields' => '',
                                'joins' => array('type' => 'INNER'),
                                'order' => '',
                                'counterCache' => '')

    );

}


class Report extends AppModel {

    var $name = 'Report';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => ''),

            'Country' => array('className' => 'Country',
                                'foreignKey' => '',
                                'conditions' => 'Report.country_id = 
Country.id',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


class Country extends AppModel {

    var $name = 'Country';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'country_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => ''),

            'Report' => array('className' => 'Report',
                                'foreignKey' => 'country_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


class Lookup extends AppModel {

    var $name = 'Lookup';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


class Rank extends AppModel {

    var $name = 'Rank';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


class Piece extends AppModel {

    var $name = 'Piece';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


class Comment extends AppModel {

    var $name = 'Comment';

    var $belongsTo = array(

            'Benchlink' => array('className' => 'Benchlink',
                                'foreignKey' => 'id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'counterCache' => '')

    );

}


?>

----------------------------------------------------------------------

Maybe you need to use Uses or Loadmodels. i dont know.

For me works for saving multiple models, theres another way to
do this without using: $lastInsertId = 
$this->Benchlink->getLastInsertId(); ???


Thnaks in advance.



francky06l escribió:
> 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
>>     
> >
>
>   


-- 
 
-----------------------------------------

José Pablo Orozco Marín
[EMAIL PROTECTED]

Tel. +506 820-7280

Por favor, visite mi pasión:
http://www.SukiaLogic.com/


--~--~---------~--~----~------------~-------~--~----~
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