How to disable behavior in symfony / doctrine?

I have doctrine softdelete behavior tied to all my models. Is there a way to delete a specific entry?

In cakephp, I remember canceling the behavior ... by deleting the entry, and then adding the behavior again.

Is there something similar in symfony / doctrine? If so, how do I disable the behavior?

Greetings

+6
php symfony1 doctrine
source share
6 answers

I think I'll go to Zed, but for completeness:

The event listener method for deletion (and selection) for soft deletion behavior contains:

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

This means that if you explicitly specify a field in the request, it will not apply to the request.

So if you do this:

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

he will not apply soft deletion materials and will actually delete the record. Note that you can do anything with t.deleted, I just did something that will always be true. An alias (β€œt.”) Is also important for its operation.

This trick also works for the choice in which I used to use it before.

As I said, I think it’s better to do it:

 $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 manually create a query. One plus for the query method is that if you have other behaviors attached to the record, they will still be respected.

+9
source share

umm .. SoftDelete's behavior includes a much better way to do this ... just call

 $record->hardDelete(); 
+13
source share

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

This will disable all record listeners for this object.

+5
source share

Try calling it, it should turn off handling behavior.

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

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

+1
source share

link text I would think that this function and setting the use of dql callbacks to false, as a manager, should do the trick :).

+1
source share

I wanted to agree with Joshua Cody that the best way is to use

 $record->hardDelete() 

However, I also wanted to add here, as this is one of the first results on google to remove behavior in the doctrine, that the easiest way to separate the behavior for "selects" is to simply include "deleted_at" (or something else that you have the name of your field, as in the request.The listener looks to see if it is turned on and if it does not delete deleted records.

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

will return deleted records.

+1
source share

All Articles