我有兩個模型與Cake 2.x中的Has and Belongs To Many Association關聯。我試圖將我的應用程序移植到Cake 3.3。一般來說,我遇到了複製Cake 2.x關聯中的「keepExisting」功能的問題。現在,我試圖使用「通過」功能,但我不確定我是在咆哮正確的樹。CakePHP 2x至3x,HABTM with keepExisting
我有以下表格:
class ContestsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('contests');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Events', [
'through' => 'ContestsEvents'
]);
}
}
class EventsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('events');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Events', [
'saveStrategy' => 'append',
'through' => 'ContestsEvents'
]);
}
}
class ContestsEventsTable extends Table
{
public function initialize(array $config) {
$this->belongsTo('Contests');
$this->belongsTo('Events');
}
}
當我保存相關事件的最初較量中,保存優良和協會記錄在連接表中創建。當我編輯比賽記錄並更改事件列表時,將添加任何選定的事件...但不會刪除未選中的舊事件。第二個問題是爲已有的關係創建新的連接記錄。我想保留已有的記錄而不是創建新的記錄。在後面的應用程序中,我將在另外一個關係中使用聯合記錄ID ...並且我不希望每次覆蓋比賽記錄更新時都要重新生成id。
我希望我在描述我想要做的事情時有意義。但是,以最簡單的形式,我試圖複製通過Cake 2X中包含的'unique'=>'keepExisting'選項發現的功能,但是無法破解如何在Cake 3.3中完成此操作的代碼?
在此先感謝您的幫助。