我有两个模型与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中完成此操作的代码?
在此先感谢您的帮助。