Wednesday, October 1, 2008

Problem using SaveAll() don't saving related model

Hello,
I have two models : Account -> hasOne -> Company & Company -> belongsTo -> Account.

on /app/models/account.php
====================
class Account extends AppModel {
     var $name = 'Account';.
     var $hasOne = array('Company');
 }  

on /app/models/company.php
=======================
class Company extends AppModel {
     var $name = 'Company';
     var $belongsTo = array('Account');
 }  

on /app/views/company.ctp
=======================
echo $form->create(Company, array('action'=>'add'));
echo $form->input('Company.name', array('label'=>'Company name'));
echo $form->input('Account.name');
echo $form->end('Add');

On /app/controllers/companies_controller.php
=============================================
function add() {
              if(!empty($this->data)) {
                  if($this->Company->saveAll($this->data, array('validate'=> true))) {
                      $this->Session->setFlash('New post added succesfully!');
                  } else {
                  $this->Session->setFlash('Failed when add new post, please try again.');
              }   
          }
 }

The problem, If new data inserted, foreignKey account_id in companies table have null value.:

Table companies :
=============
id | name | account_id
1  | test   |  0

Table accounts :
==============
id | name
1  | testing  

How to solve this problem using saveAll? or save maybe? (this is same cases with http://manual.cakephp.org/view/75/Saving-Your-Data )

Thanks ..

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: