If anyone wants...
  Here is the function I did to put into AppModel class, so that any
other model can use it if needed (just call $this->saveHasMany() into
Model::afterSave function)...
  Grab at will... =)

        /**
         * Checks if the model has 'hasMany' relationships. For each of the
relationships,
         * it deletes the old data and inserts new ones based on the
information passed
         * in $this->data['ModelName']. It should be an array of arrays with
data. Like this:
         * <pre>
         * [hasManyModel] => Array
         * (
         *   [0] => Array
         *   (
         *     [name] => Field one
         *     [size] => 5
         *   )
         *   [1] => Array
         *   (
         *     [name] => Field two
         *     [size] => 1
         *   )
         * )
         * </pre>
         *
         * @return boolean true if no errors were found in the insert
queries.
         */
        function _saveHasMany() {
                $founderror = false;
                foreach ($this->hasMany as $k => $v) {
                        // TODO: Find out how to print an error if there is no 
transaction
center

                        // Delete old data
                        $myid = $this->data[$this->name][$this->primaryKey];
                        $deletequery = "DELETE FROM {$this->$k->useTable} WHERE
{$v['foreignKey']} = {$myid}";
                        $db =& 
ConnectionManager::getDataSource($this->$k->useDbConfig);
                        $db->query($deletequery);


                        if (!isset($this->data[$k])) {
                                // No centers
                                continue;
                        }
                        if (empty($this->data[$k])) {
                                // No data
                                continue;
                        }

                        // Save new data
                        foreach ($this->data[$k] as $tc) {
                                $tc[$v['foreignKey']] = $myid;
                                $data[$k] = $tc;
                                if (!$this->$k->save($data)) {
                                        $founderror = true;
                                }
                        }
                }
                return ($founderror);
        }

Regards,


On Aug 30, 11:05 am, LS <[EMAIL PROTECTED]> wrote:
> Phang,
>   Your solution hasnt solved it.
>   I dug into the model.php function, and there is no support to add
> hasmany relations automagically. only hasandbelongstomany.
>   I've added a afterSave() function into the Transaction model...
>   For transparency, I need to check the $hasMany array, find the name
> of the tables, and make that code more generic so I can use it in
> other models without change into the model...
>
>   Here it goes:
>
>     function afterSave($created) {
>         // TODO: Find out how to print an error if there is no
> transaction center
>         if (!isset($this->data['TransactionCenter'])) {
>                 // No centers
>                 return;
>         }
>         if (empty($this->data['TransactionCenter'])) {
>                 // No data
>                 return;
>         }
>
>         $myid = $this->data['Transaction']['id'];
>
>         $deletequery = "DELETE FROM {$this->TransactionCenter->useTable}
> WHERE transaction_id = {$myid}";
>
>         $db =& ConnectionManager::getDataSource($this->useDbConfig);
>         $db->query($deletequery);
>         foreach ($this->data['TransactionCenter'] as $tc) {
>                 $tc['transaction_id'] = $myid;
>                 $data['TransactionCenter'] = $tc;
>                 $this->TransactionCenter->save($data);
>         }
>     }
>
> Thanks for your help, anyway... =)
>
> On Aug 30, 7:44 am, LS <[EMAIL PROTECTED]> wrote:
>
> > Hum...
> > Nice...
> > I'm gonna try that... But... When I do the $this->Transaction->findAll() it 
> > returns everything to me as expected.... Thats why I
>
> > never tought of expanding the hasMany parameter... I'll post back with
> > my results! =)
> > If that does not do the trick, I'll just add a AfterSave function to
> > the model...
>
> > Thanks!
>
> > On Aug 30, 1:21 am, "Phang Mulianto" <[EMAIL PROTECTED]> wrote:
>
> > > i think you miss something...
> > > in the model... you should add more param in the has many and belongs
> > > to so the update and delete is automaticaly done by cake..
>
> > > here is my example code model :
>
> > > * @package            cake
> > > * @subpackage        cake.app.config
> > > * @since            CakePHP(tm) v 0.2.9
> > > * @version            $Revision: 4409 $
> > > * @modifiedby        $LastChangedBy: phpnut $
> > > * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
> > > * @licensehttp://www.opensource.org/licenses/mit-license.phpTheMITLicense
> > > */
> > > class Mutation extends AppModel {
> > >    var $name = 'Mutation';
>
> > >    var $hasMany = array('Transaction' =>
> > >                          array('className'     => 'Transaction',
> > >                                'conditions'    => '',
> > >                                'order'         => '',
> > >                                'limit'         => '10',
> > >                                'foreignKey'    => 'mutation_id',
> > >                                'dependent'     => true,
> > >                                'exclusive'     => false,
> > >                                'finderQuery'   => ''
> > >                          )
> > >                   );
>
> > > }
>
> > > ?>
>
> > > <?php
> > > /*
> > > *
> > > [EMAIL PROTECTED]
> > > * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
> > > * @linkhttp://www.cakefoundation.org/projects/info/cakephpCakePHP(tm)
> > > Project
> > > * @package            cake
> > > * @subpackage        cake.app.config
> > > * @since            CakePHP(tm) v 0.2.9
> > > * @version            $Revision: 4409 $
> > > * @modifiedby        $LastChangedBy: phpnut $
> > > * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
> > > * @licensehttp://www.opensource.org/licenses/mit-license.phpTheMITLicense
> > > */
> > > class Transaction extends AppModel {
> > >    var $name = 'Transaction';
>
> > >   var $belongsTo = array('Mutation' =>
> > >                            array('className'  => 'Mutation',
> > >                                  'conditions' => '',
> > >                                  'order'      => '',
> > >                                  'foreignKey' => 'mutation_id'
> > >                            )
> > >                      );
>
> > > }
>
> > > ?>
>
> > > you need to add the foreignkey array variable...
>
> > > hope this help..i also tryin to figure it out on my own...hope helps..
>
> > > On 8/30/07, LS <[EMAIL PROTECTED]> wrote:
>
> > > > Hello, everyone!
>
> > > > I would like a little help from you guys, if I may...
>
> > > > I have 3 tables, wich are linked to each other with a "middle"
> > > > controller...
>
> > > > Transaction - TransactionCenter - Center
>
> > > > These are the models:
>
> > > > class Transaction extends AppModel {
> > > >    var $name = 'Transaction';
> > > >    var $belongsTo = array('Company', 'Person');
> > > >    var $hasMany = array('TransactionCenter');
> > > > }
>
> > > > class TransactionCenter extends AppModel {
> > > >    var $name = 'TransactionCenter';
> > > >         var $belongsTo = array('Transaction', 'Center');
> > > > }
>
> > > > class Center extends AppModel {
> > > >     var $name = 'Center';
> > > >     var $hasMany = 'Transaction';
> > > >     var $belongsTo = 'Account';
> > > > }
>
> > > > When the controller asks the model to save (with $this->Transaction-
> > > > >save($this->data)), it saves successfully, but only the Transaction
> > > > model. Not the TransactionCenter.
>
> > > > When I as a print_r($this->data), before saving, here's what I get:
>
> > > > Array
> > > > (
> > > >     [Transaction] => Array
> > > >         (
> > > >             [id] => 1
> > > >             [description] => Sistema Construtora
> > > >             [doc] =>
> > > >             [value] => 2200.00
> > > >             [due_date] => 2007-08-01
> > > >             [person_id] => 1
> > > >         )
>
> > > >     [TransactionCenter] => Array
> > > >         (
> > > >             [0] => Array
> > > >                 (
> > > >                     [transaction_id] => 1
> > > >                     [amount] => 1500.00
> > > >                     [porcentage] =>
> > > >                     [center_id] => 1
> > > >                 )
>
> > > >             [1] => Array
> > > >                 (
> > > >                     [transaction_id] => 1
> > > >                     [amount] => 250.00
> > > >                     [porcentage] =>
> > > >                     [center_id] => 2
> > > >                 )
>
> > > >             [2] => Array
> > > >                 (
> > > >                     [transaction_id] => 1
> > > >                     [amount] => 155.55
> > > >                     [porcentage] =>
> > > >                     [center_id] => 3
> > > >                 )
>
> > > >             [3] => Array
> > > >                 (
> > > >                     [transaction_id] => 1
> > > >                     [amount] => 555.22
> > > >                     [porcentage] =>
> > > >                     [center_id] => 4
> > > >                 )
>
> > > >         )
>
> > > > )
>
> > > > Can anyone give me a hand? I've been bumping into the wall with this
> > > > for quite some time...
>
> > > > I already have data in Transaction and TransactionCenter tables. I
> > > > added some custom data directly into the database to have a test for
> > > > my layout and such. I am now trying to make the edit action to work to
> > > > later make the add action.
>
> > > > The CREATE TABLE statements:
>
> > > > CREATE TABLE  `transactions` (
> > > >   `id` int(10) unsigned NOT NULL auto_increment,
> > > >   `created` datetime NOT NULL,
> > > >   `modified` datetime NOT NULL,
> > > >   `company_id` int(10) unsigned NOT NULL,
> > > >   `person_id` int(10) unsigned NOT NULL,
> > > >   `center_id` int(10) unsigned NOT NULL,
> > > >   `doc` varchar(50) collate utf8_unicode_ci default NULL,
> > > >   `due_date` date default NULL,
> > > >   `value` decimal(10,2) default NULL,
> > > >   `description` varchar(200) collate utf8_unicode_ci NOT NULL,
> > > >   PRIMARY KEY  (`id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
>
> > > > CREATE TABLE  `transaction_centers` (
> > > >   `transaction_id` int(10) unsigned NOT NULL,
> > > >   `center_id` int(10) unsigned NOT NULL,
> > > >   `amount` decimal(18,2) NOT NULL,
> > > >   `porcentage` decimal(18,2) default NULL,
> > > >   PRIMARY KEY  (`transaction_id`,`center_id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
>
> > > > CREATE TABLE  `centers` (
> > > >   `id` int(10) unsigned NOT NULL auto_increment,
> > > >   `created` datetime NOT NULL,
> > > >   `modified` datetime NOT NULL,
> > > >   `company_id` int(10) unsigned NOT NULL default '1',
> > > >   `account_id` int(10) unsigned default NULL,
> > > >   `name` varchar(250) collate utf8_unicode_ci NOT NULL,
> > > >   `startdate` date NOT NULL,
> > > >   `enddate` date default NULL,
> > > >   `person_id` int(10) unsigned default NULL,
> > > >   `protocol` varchar(250) collate utf8_unicode_ci default NULL,
> > > >   `number` varchar(250) collate utf8_unicode_ci default NULL,
> > > >   `description` text collate utf8_unicode_ci,
> > > >   PRIMARY KEY  (`id`),
> > > >   UNIQUE KEY `UN_COMPANYID_NAME` (`company_id`,`name`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
>
> > > > Thanks everyone.
>
> > > > - LS


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