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.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' => ''
> > )
> > );
>
> > }
>
> > ?>
>
> > <?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.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 = '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
-~----------~----~----~----~------~----~------~--~---