Yii Framework Forum: before/after delete - strange behavior - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

before/after delete - strange behavior Rate Topic: -----

#1 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,695
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 02 May 2009 - 03:15 PM

I've noticed that beforeDelete and afterDelete callbacks are fired only if we use $record->delete().

deleteAll() and deleteByPk() do the raw sql query, without any triggers.

Is it by design?

Actually, I want related records to be deleted when the main record is deleted.
I cannot do it by DB constraints (on delete cascade) because of some extra things that should be done before the record is deleted (for example, unlink some attached files, write data to log and so on).

Should I grab all related records and then call delete() on each of them? Or there's a better way?

PS. extra param in relation definition (like 'dependent' => true) that allows related records to be deleted automatically would be sooooo nice.. )))
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 May 2009 - 04:31 AM

I think, this is by design. deleteAll() can use the much faster 'DELETE FROM ...' statement. Like you said, you'll have to fetch all records first and call delete() on them.

Maybe a behavior would be a nice option so that developers can add this on demand? It could add two new "enhanced" methods (bad name suggestion: fullDeleteAll() and fullDeleteByPk()...) that fetch all records and call delete().
0

#3 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,695
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 03 May 2009 - 04:42 AM

I agree on deleteAll(), because there's no way to delete related record without grabbing list of IDs first. But the behavior of deleteByPk() was quite unexpected for me.
Should I really use two steps approach (findByPk() + delete())? Isn't it a little bit redundant?
Seems like I've missed something and there's much better way.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 May 2009 - 04:50 AM

Well, this might be true for related records. But you can do much more in beforeDelete()/afterDelete() than only delete related records. That's why the same is true for deleteByPk(): You would need another "SELECT..." to fetch the record attributes. Otherwhise attributes wouldn't be available in the before/afterDeletes.

Maybe the easier approach for you would be to simply add a method like "deleteWithRelated()" and delete related records from there manually, before you delete the records in your main table.
0

#5 User is offline   MichaelH 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 09-February 10

Posted 06 December 2011 - 06:10 PM

Thank you so much for the information Mike!
It seems logic that you first need to load the record or otherwise attributes wont be available.

Searched quite a while before I found this topic on why afterDelete() was not called after calling deleteByPk()
I think this should be in the documentation/guide.
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 08 December 2011 - 12:03 AM

The logic is simple. delete() is applied to the current model instance and deleteAll()/deleteByPk() are applied to many table rows without creating model instances, so there is no chance before/after delete events will be raised. But I agree it should be mentioned in API.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users