I have a Cakephp 1.3 app working and I have the following setup:
A table called emissions with this model:
class Emission extends AppModel
{
var $name = 'Emission';
var $displayField = 'name';
}
And a table called emission_messages with this model:
class EmissionMessage extends AppModel
{
var $name = 'EmissionMessage';
var $belongsTo = array
(
'Emission' => array
(
'className' => 'Emission',
'foreignKey' => 'emission_id'
)
);
}
The emission_message table has a field called emission_id for the foreign key.
When I create a new instance of emission I need to create a new instance of emission_message at the same time, that is: in the form where I would insert a new emission in my database I will need to have inputs of the emission_message associated with that same emission. Same goes to the edit form.
I think I did this the wrong way, because I can "manually" insert into the emission_message table with the related id once the emission is created but I'm guessing this is not the right way and that cakePHP is supposed to do this automatically. I don't know how to name my inputs on the form so that the information gets saved properly or if my models are wrong and that's why it doesn't work.
EDIT:
To make things clearer: Emission and EmissionMessage are in a one to one relationship, adding hasOne to Emissions made it work half way, I can now save the fields using:
echo $this->Form->input('EmissionMessage.field');
But when I try to update the same record the edit() action it just saves the data in a new row (with the foreign key, but in a new row, instead of updating the previous one)
To save the fields I use:
$this->Emission->saveAll($this->data)
Both in edit() and add() actions (insert and update)
EDIT 2: It seems the hidden input with the id of the EmissionMessage table did the trick (I was not doing it correctly before, once I fixed that, it worked fine) as this answer suggests: saveAll() inserts new row instead of updating
I find it very odd that I need to add that hidden input but at least that solves the problem.