我应该犯一个大错误,因为我已经多次这样做了,但我真的不明白为什么删除关联只是部分工作:
我的实体被宣布为:
class SitemessagesTable extends Table {
public function initialize(array $config) {
$this->table('sitemessages');
$this->displayField('title');
$this->addBehavior('Timestamp');
$this->hasMany('SitemessageTitles', [
'dependent' => true,
]);
}
}
class SitemessageTitlesTable extends Table {
public function initialize(array $config) {
$this->table('sitemessage_titles');
$this->displayField('title');
$this->belongsTo('Sitemessages');
$this->hasOne('SitemessageTexts', [
'dependent' => true,
]);
$this->hasOne('SitemessageImages', [
'dependent' => true,
]);
}
}
class SitemessageTextsTable extends Table {
public function initialize(array $config) {
$this->table('sitemessage_texts');
$this->displayField('text');
$this->belongsTo('SitemessageTitles');
}
}
class SitemessageImagesTable extends Table {
public function initialize(array $config) {
$this->table('sitemessage_images');
$this->displayField('image');
$this->belongsTo('SitemessageTitles');
}
}
所以我尝试删除这样的Sitemessages实体:
$sitemessage = $this->Sitemessages->get($sitemessage_id);
$this->Sitemessages->delete($sitemessage);
然后Sitemessages
和第一级SitemessageTitles
个被删除,但不会删除第二级SitemessageTexts
和SitemessageImages
个。
我不明白,因为医生说:
删除实体时,也可以删除关联数据。如果您的HasOne和HasMany关联被配置为依赖关系,则删除操作也会“级联”到这些实体。
有什么想法吗?
此致