views:

689

answers:

5

Hi,

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

+1  A: 

Try calling this, it should disable the behavior handling.

$manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, false);

As a dirty way you can generate an SQL query that deletes the entry from the table.

Zed
nah.. i think that is dirty!!.. thanks anyway though..
Yash
OK. Try the call I added. Haven't tried it, but I guess it should work.
Zed
cool.. will give that a go...
Yash
This is still a dirty way. It would disable callbacks for the rest of the request!
jeremy
+1  A: 

link text i would think that this function and setting the use dql callbacks to false just like on the manager should do the trick :).

Henrik Bjørnskov
+1  A: 

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.

benlumley
You should probably be storing the previous value of Doctrine::ATTR_USE_DQL_CALLBACKS rather than setting to false and true. What if CALLBACKS was off before that code was encountered?
jeremy
yes ... i should... edited.
benlumley
(although if callbacks are off, soft delete doesn't work)
benlumley
+5  A: 

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

$record->hardDelete();
Joshua Coady
yes - recommend this is accepted ahead of my idea. I have since started using this.
benlumley
A: 

Wanted to agree with Joshua Coady that the best way would be to use

$record->hardDelete()

However, I also wanted to add here since it's one of the first results on google for detaching the behavior in doctrine that the easiest way to detach the behavior for "selects" is simply to include "deleted_at" (or whatever you have named your field as in the query. The listener looks to see if it is included and if so does not filter deleted records out.

Doctrine_Core::getTable('Record')->createQuery()->select('id, etc1, etc2')->addSelect('deleted_at')->execute();

will return deleted records.

isleshocky77