Hi,

If you are doing 'add' operation on a model using saveAll() then belongsTo
related model doesn't get added automatically, so either you need to change
relation to Account hasOne Address (which is correct IMO) or do saveAll() on
Address model as Address hasOne/hasMany Account or the dirty way is to first
add address and then account by doing 2 save() calls on respective models.

Amit Badkas

PHP Applications for E-Biz: http://www.sanisoft.com



On Mon, Dec 6, 2010 at 1:06 PM, Mayank <[email protected]> wrote:

> Hi, I have two tables, one 'accounts' and other 'addresses'. The
> accounts models has a belongsTo to address model. At the database
> level there is a foreign key for addresses table in the accounts
> table, and when I add a new account, I also want to update the address
> table with the new address and insert a corresponding foreign key in
> to the accounts table.
>
> When I do a saveAll the account information successfully goes into the
> accounts table however, the address table is not updated and no
> address is stored in the address table.
>
> What am I doing wrong?
>
> My codes are:
>
>
> /* Account Model */
> class Account extends AppModel {
>        var $name = 'Account';
>
>        var $belongsTo = array(
>        'Group' =>
>            array('className' => 'AccountGroup'
>        ),
>        'Address' =>
>                array('className' => 'Address'
>        )
>     );
> }
>
> /* Address Model */
> class Address extends AppModel {
>        var $name = 'Address';
> }
>
> /* The add function in the controller */
> function add() {
>        $this->set('groups', $this->Account->Group->find('list'));
>        if (!empty($this->data)) {
>                $success = $this->Account->saveAll($this->data);
>        }
> }
>
> /* MySQL Tables are as follows */
> CREATE TABLE `accounts` (
>  `id` int(11) NOT NULL AUTO_INCREMENT,
>  `name` varchar(45) NOT NULL,
>  `print_name` varchar(45) DEFAULT NULL,
>  `group_id` int(11) DEFAULT NULL,
>  `address_id` int(11) DEFAULT NULL,
>  `opening_balance` float DEFAULT '0',
>  `opening_balance_debit` tinyint(1) DEFAULT '0',
>  `prev_year_balance` float DEFAULT '0',
>  `prev_year_balance_debit` tinyint(1) DEFAULT '0',
>  PRIMARY KEY (`id`),
>  KEY `group_fk` (`group_id`),
>  KEY `address_fk` (`address_id`)
> ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1$$
>
>
> delimiter $$
>
> CREATE TABLE `addresses` (
>  `id` int(11) NOT NULL AUTO_INCREMENT,
>  `address` text NOT NULL,
>  `city` varchar(45) DEFAULT NULL,
>  `state` varchar(45) DEFAULT NULL,
>  `zip` int(11) DEFAULT NULL,
>  `phone` varchar(15) DEFAULT NULL,
>  `fax` varchar(15) DEFAULT NULL,
>  `mobile` varchar(15) DEFAULT NULL,
>  `email` varchar(100) DEFAULT NULL,
>  `contact_person` varchar(45) DEFAULT NULL,
>  `pan` varchar(45) DEFAULT NULL,
>  `ward` varchar(45) DEFAULT NULL,
>  `cst` varchar(12) DEFAULT NULL,
>  `tin` varchar(45) DEFAULT NULL,
>  `service_tax_no` varchar(45) DEFAULT NULL,
>  PRIMARY KEY (`id`)
> ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1$$
>
>
> Any help will be much appreciated!
>
> Thank you!
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others
> with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<cake-php%[email protected]>For
>  more options, visit this group at
> http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" 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