我在这个问题上挣扎了一段时间。我有三个表prestation
,tools
和prestation_tools
。我的实体链接如下:
#ToolsTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tools');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsToMany('Prestation', [
'joinTable' => 'prestation_tools',
'foreignKey' => 'tools_id'
]);
}
#PrestationTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('prestation');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsToMany('Tools', [
'through' => 'PrestationTools'
]);
$this->belongsToMany('Competences', [
'through' => 'PrestationCompetences'
]);
}
#PrestationToolsTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('prestation_tools');
$this->displayField('prestation_id');
$this->primaryKey(['prestation_id', 'tools_id']);
$this->belongsTo('Prestation', [
'foreignKey' => 'prestation_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Tools', [
'foreignKey' => 'tools_id',
'joinType' => 'INNER'
]);
}
我的表格是:
<?= $this->Form->input("Prestation.Tools.1.tools_id", [
'label' => false,
'type' => 'select',
'options' => $tools,
'empty' => "Sélectionner l'outil 1",
'templates' => [
'inputContainer' => '{{content}}'
],
'class' => 'form-control',
]) ?>
<label >Spécifications de l'outil 1</label>
<?= $this->Form->textarea('Prestation.Tools.1.details', [
'templates' => [
'inputContainer' => '<div class="form-group">{{content}}</div>'
],
'class' => 'form-control',
'placeholder' => 'Spécifications de l\'outil',
'label' => 'Spécifications de l\'outil'
]) ?>
尝试从PrestationController
保存时,会保存prestation
个记录。任何人都可以帮助我吗?
更新
我的PrestationController
add()
操作如下所示:
public function add(){
#...
$prestation = $this->Prestation->patchEntity($prestation, $this->request->data, [
'associated' => [
'Tools',
'Competences'
]
]);
#...
}