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?


-------------Problems Reply------------

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(' = ? 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);
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.

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



This will disable all record listeners for this object.

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


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.

Category:php Views:2 Time:2009-09-24

Related post

  • How to Detach a Behavior from an UIElement in Code Behind for Silverlight? 2010-02-25

    In Silverlight 3.0 I have added a custom behavior to some UIElement in Code Behind. I wanted to remove the Behavior later in runtime. What is the C# syntax to Detach an already added Behavior from an UIElement? --------------Solutions------------- I

  • Symfony doctrine i18n behavior data-load failure: Integrity constraint violation 2011-01-14

    I want tho have I18N categories table. I have followed the jobeet example. Schema and fixtures data below. ./symfony doctrine:build --db --all-classes --and-migrate ./symfony doctrine:data-load data/fixtures/category.yml When I run these commands no

  • Symfony - How can I insert behaviors in my doctrine migrations? 2009-04-28

    Is it possible to add behaviors (eg. actAs Timestampable) in a doctrine migration to avoid defining the created_at and updated_at columns ? Thank's in advance ! --------------Solutions------------- I think there is no way to add behaviors in the migr

  • In symfony/doctrine's schema.yml, where should I put onDelete: CASCADE for a many-to-many relationship? 2009-09-28

    I have a many-to-many relationship defined in my Symfony (using doctrine) project between Orders and Upgrades (an Order can be associated with zero or more Upgrades, and an Upgrade can apply to zero or more Orders). # schema.yml Order: columns: order

  • unit testing with symfony + doctrine + PHPUnit 2009-09-10

    I'm working with Symfony + Doctrine + PHPUnit, with NetBeans IDE. Here' my current approach to unit testing. setUp() function loads the test fixtures from .yml files tearDown() function delete all data from models. this is done by looping through an

  • how to resolve symfony doctrine:build-schema error (Unknown relation alias table_name) 2009-10-26

    how to resolve this symfony error : C:\inetpub\wwwroot\project\trunk\preprod\signup>php symfony doctrine:build-schema --trace >> doctrine generating yaml schema from database [sfException] Unknown relation alias table_name Exception trace: a

  • Standard idiom for adding new models to an app built on Symfony + Doctrine 2009-12-08

    What is the a standard way of adding new model to my app built on Symfony + Doctrine while maintaining all previous models and their meta-data (like relationships). What am I really looking for: A command / procedure that will be equivalent of ./scri

  • symfony doctrine build-sql error 2010-04-10

    I have some big problems with symfony and doctrine at the beginning of a new project. I have created database diagram with mysql workbench, inserted the sql into phpmyadmin and then I've tried symfony doctrine:build-schema to generate the YAML schema

  • symfony doctrine:insert-sql - "Couldn't locate driver named mysql" error 2010-04-28

    hey, I am a newbie in symfony. I am following this joobet tutorial on, I am on there day 3 Whenever I type php symfony doctrine:insert-sql, I get the following error: doctri

  • symfony + doctrine + inheritance, how to make them work? 2010-05-05

    I am beginning to work with Symfony, I've found some documentation about inheritance. But also found this discouraging article, which make me doubt if Doctrine handles inheritance any good at all... Has anyone find a smart solution for inheritance in

  • How to make 'symfony doctrine:build-sql' task generate 'DROP' statments? 2010-09-02

    Is there a way to make symfony doctrine:build-sql task generate DROP statments before CREATE TABLE ones as it symfony propel:build-sql does ? ? --------------Solutions------------- There is no such possibility, but you can run the doctrine:drop-db ta

  • Symfony doctrine:generate-module custom theme: cannot find templates for i18n embedded field 2010-09-05

    I generated a custom theme by copying plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default" into plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/mytheme1" I changed the templates to match my needs (no tables, custom errors displa

  • Altering a database in symfony (doctrine) 2010-09-16

    I'm a beginner with symfony (1.4 + Doctrine), but there is a point which scares me. It seems that whenever one wants to change a model, the only way is to change the schema for the database (config/doctrine/schema.yml) and then call symphony doctrine

  • Symfony Doctrine skeleton files 2010-10-22

    I'm using Symfony 1.4 with the Doctrine 1.2 plugin. I would like to add some function to the Doctrine auto-generated models files (*Base.class.php). Thoses file are normally generated using "./symfony doctrine:build-model" command from the CLI. I kno

  • Symfony doctrine:build-model errors 2010-11-07

    I have written my yaml schema for the database for my Symfony+doctrine application, and I'm trying to get it to make the models and such, but it errors on doctrine:build-model $> ./symfony doctrine:build-model >> doctrine generating model cl

  • Symfony+Doctrine: Correct YAML syntax for default value of boolean field 2010-11-21

    I am working on the schema for my Symfony app, and I need to set the default value of two boolean fields to false. However, with all the ways I've tried to do it, when the sql gets generated, it comes out with the default keyword, but no default valu

  • Expected path to Sluggable builder in Symfony/Doctrine 2010-12-02

    The default string normalization (provided by the Doctrine_Inflector::urlize() "sluggifier") isn't suitable for my project, so I need to utilize my own algorithm. I set my schema up as follows: MyObject: actAs: Sluggable: fields: [name] builder: arra

  • Symfony doctrine and stored procedure 2011-01-27

    I try to learn symfony by this tutorial And is very good guide. But know im want to create stored procedure. And how i can describe procedures in shema yaml files? Because when i try to ./symfony doctrine:build --all -and-load - doctrine drop my data

  • symfony doctrine admin generator sort by virtual columns 2011-02-04

    I'm working with the Symfony doctrine admin generator. I've added a couple of virtual columns as detailed on this page What I want to be able to do is sort my list based on those virtual column

Copyright (C), All Rights Reserved.

processed in 0.181 (s). 11 q(s)