Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

doctrine,application level cascade: [delete] does not work

i am using doctrine's SoftDelete and upon deletion i would like to SofDelete the related records.

here is the Schema file i am using.

detect_relations: true
User:
  actAs:
    Timestampable:
    SoftDelete:
    Sluggable:
      unique: true
      fields: [name]
      canUpdate: true
  tableName: user
  columns:
    name:
      type: string(50)
      notnull: true
    email:
      type: string(50)
      notnull: true
      unique: true
    password:
      type: string(50)
      notnull: true
    business_id: integer
  relations:
    Business:
      cascade: [delete]
Business:
  actAs:
    Timestampable:
    SoftDelete:
    Sluggable:
      unique: true
      fields: [name]
      canUpdate: true
  tableName: business
  columns:
    name:
      type: string(50)
      notnull: true
    website: string(100)
    address: string(100)

when i try to SoftDelete user it does not delete related record from business table (i.e it does not update the deleted_at flag in the business table). only the deleted_at flag from the user table is updated.

the DQL i am using is.

$q = Doctrine_Query::create()
    ->delete('Model_User u')
    ->where('u.id = ?', $id);
$q ->execute();

where am i going wrong?

Upvotes: 1

Views: 807

Answers (1)

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

After searching around for some time, i finally found the solution by myself. hope this helps someone with the same problem as mine.

here are few of the conditions for application level delete cascade to work.

  • Unlike the save() operations the delete() cascading needs to be turned on explicitly

apart from defining cascade: [delete] in your Schema, it is important to note that you need to explicitly define the relations too.
if you are depending on detect_relations: true for you to generate all the relations this won't work for application level delete cascade. (atleast it didn't work for me, after manually defining the relations it worked though).

  • Application level cascading save() and delete() does not apply when doing DQL update and delete statements, only when calling save() and delete() on your objects.

doctrine documentation is not clear about this. i had to refer symfony documentation for this to understand. doctrine documentation only says.

The following describes the generic procedure when you delete a record through $record->delete():

for me to understand it was quite confusing. but after going through symfony's documentation of doctrine i was clear about how to do it. so instead of deleting through DQL. i had to use this.

$user = Doctrine_Core::getTable('User')->find(1)
$user->delete();

and Booom, it works. :)

P.S: here is the link to symfony documentation in case if anyone wants to refer.

http://www.symfony-project.org/doctrine/1_2/en/04-Schema-Files#chapter_04_sub_application_level

and here is the link to a post which clarifies my doubt which is not of any use though :)

https://groups.google.com/forum/?fromgroups#!topic/doctrine-user/POq6ybO01lg%5B1-25%5D

Upvotes: 4

Related Questions