I have successful able to save data in User and Profile model, the problem is in the Profile table only id and user_id is getting inserted, remaining fields in the profile eg, email, city are NULL. Please help me in sorting this, Please find the codes below:
Model:
user.php:
class User extends AppModel {
public $name = 'User';
var $hasOne = array('Profile' =>
array('className' => 'Profile',
'conditions' => '',
'order' => '',
'dependent' => true,
'foreignKey' => 'user_id'
)
);
.....
UsersController.php:
class UsersController extends AppController {
.....
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
if (!empty($this->request->data)) {
$this->request->data['Profile']['user_id'] = $this->User->id;
$this->User->Profile->save($this->request->data);
}
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('controller' => 'homes','action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
View: add.ctp
<?php echo $this->Form->create('User' , array('action' => 'add'));?>
<fieldset>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('profile.firstname');
echo $this->Form->input('profile.lastname');
echo $this->Form->input('profile.email');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
Model: Profile.php:
class Profile extends AppModel {
public $name = 'Profile';
var $belongsTo = array('User' =>
array('className' => 'User',
'conditions' => '',
'order' => '',
'foreignKey' => 'user_id'
)
);
....
Please help.
Thanks in advance!!!
Update your form:
Also, the following if statement is overkill:
clearly there is request data or you would not have gotten past the user save.