CakePHP - 同时更新多个表 [英] CakePHP - Updating multiple tables at the same time
问题描述
我有一个Cakephp 1.3应用程序工作,我有以下设置:
I have a Cakephp 1.3 app working and I have the following setup:
这种模型的排放表:
class Emission extends AppModel
{
var $name = 'Emission';
var $displayField = 'name';
}
这个模型有一个名为emit_messages的表:
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'
)
);
}
emit_message表有一个名为emit_id 。
The emission_message table has a field called emission_id for the foreign key.
当我创建一个新的发射实例时,我需要同时创建一个emit_message的新实例,即:在我将插入一个新的发射在我的数据库中我将需要有与相同的发射相关联的emission_message的输入。编辑表单也一样。
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.
我想我做错了方法,因为我可以手动插入到emit_message表与相关的ID一旦发射创建,但我猜这个不是正确的方式,而cakePHP应该自动这样做。我不知道如何在表单上命名我的输入,以便正确保存信息或如果我的模型错误,这就是为什么它不工作。
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.
编辑:
为了使事情更清楚:Emission和EmissionMessage是一对一的关系,添加hasOne到排放使它工作一半,我现在可以保存字段使用:
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');
但是当我尝试更新同一个记录edit()动作时,它只是将数据保存在新行(使用外键,但在新行中,而不是更新前一行)
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)
要保存我使用的字段:
$this->Emission->saveAll($this->data)
在edit()和add()动作(插入和更新)中
Both in edit() and add() actions (insert and update)
编辑2:隐藏输入与EmissionMessage表的id做的伎俩(我没有做正确之前,一旦我修正,它工作正常),因为这个答案建议: saveAll()插入新行而不是更新
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.
推荐答案
只是做的事情, -
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'
));
}
,然后生成表单。
像 -
$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.
在链接下方链接获取更多详细信息。 http://book.cakephp.org/1.3 /en/The-Manual/Developing-with-CakePHP/Models.html
go throuh below link for more detail. http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html
这篇关于CakePHP - 同时更新多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!