Doctrine cascade:[delete] does not call delete() method of related objects
I am using Doctrine 1.2 in my project. The schema.yml file contains:
Campaign:
tableName: campaign
actAs:
Timestampable:
created:
name: created_datetime
type: timestamp
format: Y-m-d H:i:s
updated:
disabled: true
columns:
id:
type: integer(9)
fixed: false
unsigned: false
primary: true
autoincrement: true
...
relations:
CampaignImages:
local: id
foreign: campaign_id
type: many
cascade: [delete]
...
CampaignImages:
tableName: campaign_images
columns:
id:
type: integer(9)
fixed: false
unsigned: false
primary: true
autoincrement: true
campaign_id:
type: integer(9)
fixed: false
unsigned: false
primary: false
开发者_JAVA百科 notnull: true
autoincrement: false
...
I have defined CampaignImages::delete() method and put some debugging code there, but it does not get executed when Campaign::delete() is called.
Isn't cascade:[delete] meant precisly for this reason? I don't want to use database level cascades, because image files associated with CampaignImage must be deleted when deleting record.
Cascade delete doesn't run delete() method. However, it guarantees that hooks are run.
If you need to implement any pre/post delete logic you shouldn't overwrite delete() method but rather use preDelete() or postDelete() hooks.
I have solved in another way
in Campaign class you put this
public function setUp() {
parent::setUp();
// to delete cascaded items
$CampaignRel = $this->_table->getRelation("CampaignImages");
$CampaignRel->offsetSet('cascade', array('delete'));
}
it did work for me that way
Aren't you looking for
onDelete: CASCADE
It sounds more like the option you want.
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/defining-models/en#relationships:foreign-key-constraints:integrity-actions
精彩评论