CakePHP - Updating multiple tables at the same time

时间秒杀一切 提交于 2019-12-12 02:45:17

问题


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.


回答1:


just do the things in the way like as --

class Emission extends AppModel
{
    var $name = 'Emission';
    var $displayField = 'name';
var $hasOne = array('EmissionMessage' => array
        (
            'className' => 'EmissionMessage',
            'foreignKey' => 'emission_id'
        ));
} 

and make the form after that.

like as --

$this->Form->create('Emission');
$this->Form->input('Emission.id'); 
$this->Form->input('Emission.fieldsname');
$this->Form->input('EmissionMessage.id');
$this->Form->input('EmissionMessage.fieldsname2');
$this->Form->input('EmissionMessage.fieldsname3');
$this->Form->submit();

and then in the action call-

$this->Emission->saveAll($this->data); for saving all the data.

go throuh below link for more detail. http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html




回答2:


If Emission has only one Emission_message, you need to use hasOne Association Type, change your model to:

class Emission extends AppModel {
    var $name = 'Emission';
    var $displayField = 'name';
    public $hasOne = array(
        'EmissionMessage' => array(
            'className' => 'Emission_message',
            'dependent' => true
        )
    );
}

and

class EmissionMessage extends AppModel {
    public $belongsTo = array(
        'Emission' => array(
            'className' => 'Emission',
            'foreignKey' => 'emission_id'
        )
    );
}



回答3:


Declare $hasMany in your Emission for making hasMany relationship with EmissionMessage

class Emission extends AppModel {
    var $name = 'Emission';
    var $displayField = 'name';

    public $hasMany = array('EmissionMessage');
}

Emission will also save the records of EmissionMessage model once you put the data array like this. i.e.

$data = array(
    'Emission' => array(
        'field1' => 'value1',
        'field2' => 'value2'
    ),
    'EmissionMessage' => array(
        '0' => array(
            'field1' => 'value1',
        ),
        '1' => array(
            'field1' => 'value1',
        ),
        '2' => array(
            'field1' => 'value1',
        )
    )
);

you can make an above array using your form helper.

$this->Form->create('Emission');
$this->Form->input('Emission.field1');
$this->Form->input('Emission.field2');
$this->Form->input('EmissionMessage.0.field1');
$this->Form->input('EmissionMessage.1.field1');
$this->Form->input('EmissionMessage.2.field1');
$this->Form->submit();

when you submit the form you can get the data like the above array in $this->request->data.



来源:https://stackoverflow.com/questions/21043445/cakephp-updating-multiple-tables-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!