CakePHP 3.x:将一个输入保存到多个表中
CakePHP 3.x: saving one input into multiple tables
抱歉,我不得不再问一遍,但我正在努力将一个输入保存到两个 table 中。
我的第一个table:
deceases
- id
- other records
- funeral_id
- cemetery_id
我的第二个table:
funerals
- id
- other records
- cemetery_id
我的第三个table:
cemeteries
- id
- other records
协会:
One decease belongs to a funeral. A funeral has many deceases.
One decease belongs to a cemetery. A cemetery has many deceases.
One funeral belongs to a cemetery. A cemetery has many funerals.
在 DeceasesController 的添加操作中,我想通过一个输入将 cemetery_id
保存到 deceases
和 funerals
中。
死亡和葬礼的其他输入保存得很好,但我也不明白 CakePHP 如何保存 cemetery_id
的文档。
死亡控制器:
public function add() {
$decease = $this->Deceases->newEntity();
if ($this->request->is('post')) {
$decease = $this->Deceases->patchEntity($decease, $this->request->data, [
'associated' => [
'Funerals',
'Cemeteries'
]
]);
if ($this->Deceases->save($decease)) {
$this->Flash->success(__('Saving successfully.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Saving wasn´t successful. Try again.'));
}
}
$cemeteries = $this->Deceases->Cemeteries->find('list', ['limit' => 300]);
$funerals = $this->Deceases->Funerals->find('list', ['limit' => 200]);
$this->set(compact('decease', 'cemeteries', 'funerals'));
$this->set('_serialize', ['decease']);
}
add.ctp 的片段:
echo $this->Form->input('funeral.cemetery_id', ['options' => $cemeteries, 'label' => 'cemetery']);
希望您能够理解我的问题。英语不是我的第一语言。
提前致谢。
希望我已经理解你的问题:
在 patchEntity
之后和保存之前,您可以在控制器中执行
$decease->cemetery_id = $decease->funeral->cemetery_id;
因此将为死亡和葬礼设置相同的值
抱歉,我不得不再问一遍,但我正在努力将一个输入保存到两个 table 中。
我的第一个table:
deceases
- id
- other records
- funeral_id
- cemetery_id
我的第二个table:
funerals
- id
- other records
- cemetery_id
我的第三个table:
cemeteries
- id
- other records
协会:
One decease belongs to a funeral. A funeral has many deceases.
One decease belongs to a cemetery. A cemetery has many deceases.
One funeral belongs to a cemetery. A cemetery has many funerals.
在 DeceasesController 的添加操作中,我想通过一个输入将 cemetery_id
保存到 deceases
和 funerals
中。
死亡和葬礼的其他输入保存得很好,但我也不明白 CakePHP 如何保存 cemetery_id
的文档。
死亡控制器:
public function add() {
$decease = $this->Deceases->newEntity();
if ($this->request->is('post')) {
$decease = $this->Deceases->patchEntity($decease, $this->request->data, [
'associated' => [
'Funerals',
'Cemeteries'
]
]);
if ($this->Deceases->save($decease)) {
$this->Flash->success(__('Saving successfully.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Saving wasn´t successful. Try again.'));
}
}
$cemeteries = $this->Deceases->Cemeteries->find('list', ['limit' => 300]);
$funerals = $this->Deceases->Funerals->find('list', ['limit' => 200]);
$this->set(compact('decease', 'cemeteries', 'funerals'));
$this->set('_serialize', ['decease']);
}
add.ctp 的片段:
echo $this->Form->input('funeral.cemetery_id', ['options' => $cemeteries, 'label' => 'cemetery']);
希望您能够理解我的问题。英语不是我的第一语言。
提前致谢。
希望我已经理解你的问题:
在 patchEntity
之后和保存之前,您可以在控制器中执行
$decease->cemetery_id = $decease->funeral->cemetery_id;
因此将为死亡和葬礼设置相同的值