简体   繁体   中英

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

嗯.. SoftDelete行为包括一个更好的方法来做这个...只是打电话

$record->hardDelete();

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.

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

这将禁用此对象的所有记录侦听器。

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.

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 :).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM