Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i detach a behavior in Symfony/Doctrine?

I have doctrine's softdelete behavior attached to all of my models. Is there a way I can hard delete a particular record?

In cakephp I remember detaching the behavior... deleting the record and then re attaching the behavior.

Is there something similar in symfony/doctrine ? If so then how do I detach a behavior?

Cheers

like image 579
Yashvit Avatar asked Sep 24 '09 14:09

Yashvit


2 Answers

umm .. the SoftDelete behavior includes a much nicer way of doing this ... just call

$record->hardDelete();
like image 178
Joshua Coady Avatar answered Oct 01 '22 15:10

Joshua Coady


Think I'd go for Zed's way, but for completeness:

The Event listener method for delete (and select) for the soft delete behaviour contains:

if ( ! $query->contains($field)) {
   // do the magic stuff to covert the query to respect softdelete
}

This means that if you explicitly mention the field in the query, it won't apply the transformation to the query.

So, if you do:

$q = Doctrine_Query::create()
->delete('Table t')
->where('t.id = ? AND t.deleted != 2 ', 1);

it won't apply the soft delete stuff and will actually delete the record. Note that you can do anything with t.deleted, I've just done something that will always be true. The alias ('t.') is important too for it to work.

This trick works for selects too, which is where I've normally used it before.

As I say though, I think its nicer to do:

$old_dqlc = Doctrine_Manager::getInstance()->getAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS);
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, false);
$record->delete();
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, $old_dqlc);

In particular, you can still use the delete() method rather than having to manually create the query. The one plus for the query method is that if you have other behaviours attached to the record, they will still be respected.

like image 24
benlumley Avatar answered Oct 01 '22 15:10

benlumley