Re: Saving with hasMany in 1.2
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: * * [hasManyModel] => Array * ( * [0] => Array * ( * [name] => Field one * [size] => 5 * ) * [1] => Array * ( * [name] => Field two * [size] => 1 * ) * ) * * * @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 : > > > > * @packagecake > > > * @subpackagecake.app.config > > > * @sinceCakePHP(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' => > > >
Re: Saving with hasMany in 1.2
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 : > > > * @packagecake > > * @subpackagecake.app.config > > * @sinceCakePHP(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.phpTheMIT License > > */ > > 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' => '' > > ) > > ); > > > } > > > ?> > > > > /* > > * > > [EMAIL PROTECTED] > > * @copyrightCopyright 2005-2007, Cake Software Foundation, Inc. > > * @linkhttp://www.cakefoundation.org/projects/info/cakephpCakePHP(tm) > > Project > > * @packagecake > > * @subpackagecake.app.config > > * @sinceCakePHP(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.phpTheMIT License > > */ > > 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 = 'Ac
Re: Saving with hasMany in 1.2
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 : > > * @packagecake > * @subpackagecake.app.config > * @sinceCakePHP(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.phpThe MIT License > */ > 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' => '' > ) > ); > > } > > ?> > > /* > * > [EMAIL PROTECTED] > * @copyrightCopyright 2005-2007, Cake Software Foundation, Inc. > * @linkhttp://www.cakefoundation.org/projects/info/cakephpCakePHP(tm) > Project > * @packagecake > * @subpackagecake.app.config > * @sinceCakePHP(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.phpThe MIT License > */ > 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] =>
Re: Saving with hasMany in 1.2
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 : * @packagecake * @subpackagecake.app.config * @sinceCakePHP(tm) v 0.2.9 * @version$Revision: 4409 $ * @modifiedby$LastChangedBy: phpnut $ * @lastmodified$Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ 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' => '' ) ); } ?> http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @packagecake * @subpackagecake.app.config * @sinceCakePHP(tm) v 0.2.9 * @version$Revision: 4409 $ * @modifiedby$LastChangedBy: phpnut $ * @lastmodified$Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ 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,
Re: Saving with hasMany in 1.2
Hey, Mech7, Thanks for the reply... But that is for when you need to add (include) things to HABTM... In my problem, I have all the required components... It could be solved with the queries DELETE FROM transaction_centers WHERE transaction_id = 1; INSERT INTO transaction_centers VALUES (.); But I wanted to find out a way for cake to do it automatically... I know it can do it with a HABTM association... but what I have there is a hasMany association (with TransactionCenter). Regards, - LS On Aug 29, 3:01 pm, Mech7 <[EMAIL PROTECTED]> wrote: > How about this > behaviour:http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior > > On Aug 29, 7:45 pm, 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 ut
Re: Saving with hasMany in 1.2
How about this behaviour: http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior On Aug 29, 7:45 pm, 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 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?hl=en -~--~~~~--~~--~--~---
Saving with hasMany in 1.2
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 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?hl=en -~--~~~~--~~--~--~---
Re: Saving with hasMany
Perhaps I need to be more specific. I am importing the data from a .csv file, so I am building the data structure myself, and the information from the Manual does not explain how to create a data structure for multiple hasMany records. So far I have managed to save Member records, and I had no trouble creating links to Categories through a hasAndBelongsToMany association. I have tried both $this->Member->Individual->save($record); and $this->Individual->save($inds_record); and neither worked, so there must be something wrong with my data structure or my code. Here is one data structure, I tried: $record = array( "Member"=>array( "id" = "", ... "Category"=>array( "Category"=>array( [0] = $category_id[0], ... ) ) "Individual"=>array( "id" = "", "individual_name" = $inds[$i] ) ) ) and it didn't work. Also even if it did work, what would happen if I looped through $i? I suspect I would end up with a bunch of duplicate Member records. So, I tried just saving Individuals: $inds_record = array( "Individual"=>array( "id" = "", "individual_name" = $inds[$i] ) ) and that didn't work either. I haven't starting using getLastInsertId() yet, but I am going to add that today, but I suspect I am just missing something simple. Any suggestions? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Saving with hasMany
On Aug 18, 2006, at 3:54 PM, Rolo D. Monkey wrote: > > I know that you cannot save associated records directly from a model > that has a hasMany relationship, but how do you do it indirectly? I > have been all over the web for hours looking for this. > > Here is a simplified version > > Model Member hasMany Individual > Model Individual belongsTo Member > > I am importing data and I can successfully save a new Member. I have > an array of Individual names, but I don't know how to save them, and > keep the association intact. Is it some variation of > > $this->Member->Individual->save($record); You should be able to do this if the two are associated. > > or do I have to declare > > $uses = array("Member", "Individual"); > > in the MembersController, and then do > > $this->Individual->save($record); This should work as well, but $record['Individual']['member_id'] needs to be populated with the right ID. > If so, how do I get the id of the member I just saved? Model::getLastInsertId() http://manual.cakephp.org/chapter/models > By the way, CakePHP is awesome, and it has already saved me weeks > worth > of work, but there are massive holes in the documentation on simple > stuff like this. Yeah, the docs have some holes, but this is addressed in the "Saving Your Data" section of the URL provided above. Hope this works for you. -- John --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Saving with hasMany
I know that you cannot save associated records directly from a model that has a hasMany relationship, but how do you do it indirectly? I have been all over the web for hours looking for this. Here is a simplified version Model Member hasMany Individual Model Individual belongsTo Member I am importing data and I can successfully save a new Member. I have an array of Individual names, but I don't know how to save them, and keep the association intact. Is it some variation of $this->Member->Individual->save($record); or do I have to declare $uses = array("Member", "Individual"); in the MembersController, and then do $this->Individual->save($record); If so, how do I get the id of the member I just saved? By the way, CakePHP is awesome, and it has already saved me weeks worth of work, but there are massive holes in the documentation on simple stuff like this. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---