根据Cakephp命名约定,我有2个表“ Descriptions”和“ Phases”,使用联接表“ DescriptionsPhases”与BelongsToMany关联。我现在一直在为此苦苦挣扎:现在我只是无法让控制器将 description 和 phase 的ID保存到我的联接表中,该表基本上只有3列,即id / description_id / phase_id。元数据可能还会更多。
这是我的代码结构:
在说明表中:
$this->belongsToMany('Phases', [
'foreignKey' => 'description_id',
'targetForeignKey' => 'phase_id',
'through' => 'DescriptionsPhases',
'saveStrategy' => 'append'
]);
在“阶段”表中:
$this->belongsToMany('Descriptions', [
'foreignKey' => 'phase_id',
'targetForeignKey' => 'description_id',
'through' => 'DescriptionsPhases'
]);
在“描述阶段”表中:
$this->belongsTo('Descriptions', [
'foreignKey' => 'description_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Phases', [
'foreignKey' => 'phases_id',
'joinType' => 'INNER'
]);
在以上所有3个实体中(目前):
protected $_accessible = [
'*' => true
];
在“描述控制器”中,使用add()方法。 $ descriptions-> phases的ID现在已经硬编码,只是为了降低复杂性:
public function add() {
$description = $this->Descriptions->newEntity([
'associated' => ['Phases']]);
if ($this->request->is('post')) {
// additional data to be saved in new description
$description->user_id = $this->Auth->user('id');
$description->designvariant = 666;
// Hardcoded integer TO BE SAVED in associated model (via join table)
$description->phases = [['id' => 5]];
$patch = $this->Descriptions->patchEntity($description, $this->request->getData(), [
'associated' => ['Phases']]);
debug($patch);
$save = $this->Descriptions->save($patch);
debug($save);
if ($save) {
$this->Flash->success(__('The description has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The description could not be saved. Please, try again.'));
}
// pass arrays for dropdown menus
$this->set('trades', ($this->Descriptions->Trades->listLabels()));
$this->set('elements', ($this->Descriptions->Elements->listLabels()));
$this->set('allocations', ($this->Descriptions->Allocations->listLabels()));
$this->set(compact('description'));
$this->render();
}
我在patchEntity和saveEntity之后都进行了调试,并获得了此数据结构。
调试($ patch);
object(App\Model\Entity\Description) {
'associated' => [
(int) 0 => 'Phases'
],
'user_id' => (int) 1,
'designvariant' => (int) 666,
'phases' => [
(int) 0 => [
'id' => (int) 5
]
],
'element_id' => 'A',
'shorttext' => 'title',
'longtext' => 'text',
'trade_id' => '0',
'allocation_id' => 'BMST',
'[new]' => true,
'[accessible]' => [
'trade_id' => true,
'element_id' => true,
'allocation_id' => true,
'shorttext' => true,
'longtext' => true,
'designvariant_id' => true,
'user_id' => true,
'created' => true,
'modified' => true,
'trade' => true,
'element' => true,
'allocation' => true,
'phase' => true,
'phases' => true,
'designvariant' => true,
'user' => true,
'amounts' => true,
'costs' => true,
'descriptions_phases' => true,
'descriptions_phase' => true,
'*' => true
],
'[dirty]' => [
'associated' => true,
'user_id' => true,
'designvariant' => true,
'phases' => true,
'element_id' => true,
'shorttext' => true,
'longtext' => true,
'trade_id' => true,
'allocation_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Descriptions'
}
调试($ save);
object(App\Model\Entity\Description) {
'associated' => [
(int) 0 => 'Phases'
],
'user_id' => (int) 1,
'designvariant' => (int) 666,
'phases' => [
(int) 0 => [
'id' => (int) 5
]
],
'element_id' => 'A',
'shorttext' => 'title',
'longtext' => 'text',
'trade_id' => '0',
'allocation_id' => 'BMST',
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2019-01-11T10:04:32+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\FrozenTime) {
'time' => '2019-01-11T10:04:32+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'id' => (int) 133,
'[new]' => false,
'[accessible]' => [
'trade_id' => true,
'element_id' => true,
'allocation_id' => true,
'shorttext' => true,
'longtext' => true,
'designvariant_id' => true,
'user_id' => true,
'created' => true,
'modified' => true,
'trade' => true,
'element' => true,
'allocation' => true,
'phase' => true,
'phases' => true,
'designvariant' => true,
'user' => true,
'amounts' => true,
'costs' => true,
'descriptions_phases' => true,
'descriptions_phase' => true,
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Descriptions'
}
记录将顺利保存到“描述表”中,但不会保存任何关联的数据。
我缺少明显的东西吗? 感谢您的任何帮助。另外,如果您需要更多信息,请询问,因为这是我的第一篇文章:-)
答案 0 :(得分:1)
仅保存实体,即phases
必须保存一个实体数组,而不是嵌套数组。嵌套数组就是您要传递给newEntity()
/ patchEntity()
的对象,在那里它们将被相应地转换为实体。
$description->phases = [
$this->Descriptions->Phases->get(5)
];
另请参见