I have a User model.
It has register view containing a form that looks like this:
echo $this->Form->create('User',array(NULL,NULL,'class' => 'signinform'));
echo $this->Form->input('first_name');
...
echo $this->Form->end('Create Account');
When you submit the form, it saves like this:
$this->User->save($this->data)
This works.
I added a table to my database called addresses
with the field user_id
that is a foreign key to users.id
I put in my Users model:
var $hasMany = 'Address';
I add fields like this to the register form:
echo $this->Form->input('Address.city');
I expected this to create a new entry into the addresses table and associate it with the new user. It does not, It creates a new user, but puts nothing in the addresses table.
I tried changing the save function from save
to saveAll
:
$this->User->saveAll($this->data)
Now nothing gets saved.
What am I doing wrong?
CakePHP saving needs a bit more work to save like that across relationships. Here's an example from the documentation.
When you make more than one database change, you should consider using transactions to make them succeed or fail together. If you don't want to use transactions, consider what you will display to the user when the request fails part way through. Also consider what state the database will be left in, and how you can recover.