fest
fest

Reputation: 1635

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
      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.

Upvotes: 0

Views: 1059

Answers (3)

Karim Samir
Karim Samir

Reputation: 1538

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

Upvotes: 1

DrColossos
DrColossos

Reputation: 12988

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

Upvotes: -2

Jakub Zalas
Jakub Zalas

Reputation: 36191

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.

Upvotes: 3

Related Questions