6

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

benlumley
  • 11,370
  • 2
  • 40
  • 39
Yashvit
  • 2,337
  • 3
  • 25
  • 32

6 Answers6

13

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

$record->hardDelete();
Tapper
  • 1,393
  • 17
  • 28
Josh Coady
  • 2,089
  • 2
  • 18
  • 25
10

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
  • 11,370
  • 2
  • 40
  • 39
  • 1
    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 Kauffman Aug 14 '10 at 02:20
  • If you want to use the first way you described make shure you call the `deleted` field with an table alias, because it's set as `$field = $params['alias'] . '.' . $this->_options['name'];` in the listener. – SBH Jun 25 '14 at 10:28
5

$object->getListener()->setOption('disabled',true);

This will disable all record listeners for this object.

gmadd
  • 1,146
  • 9
  • 18
Tobi
  • 51
  • 1
  • 1
1

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
  • 57,028
  • 9
  • 76
  • 100
1

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,581
  • 1
  • 12
  • 9
1

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.