sfPropelParanoidBehaviorPlugin plugin
The sfPropelParanoidBehaviorPlugin is a symfony plugin that provides a new Propel behavior.
If you enable this behavior for one of your model class, deletion of any object of this class is disabled and replaced with the updating of the deleted_at column of the object.
The plugin also adds a new method forceDelete to force object deletion.
Installation
- Install the plugin
symfony plugin-install http://plugins.symfony-project.com/sfPropelParanoidBehaviorPlugin
- Enable Propel behavior support in propel.ini:
propel.builder.AddBehaviors = true
If you have to enable the behavior support, rebuild your model:
symfony propel-build-model
- Activate the behavior for one of your Propel model:
// lib/model/Article.php
class Article
{
}
sfPropelBehavior::add('Article', array('paranoid'));
By default, the plugin will update the deleted_at column for this model. You can also specify another column:
sfPropelBehavior::add('Article', array('paranoid' => array('column' => 'deleted_at')));
Usage
Here are some examples of Propel calls and the generated SQL:
$article = Article::retrieveByPK(1); // SELECT blog_article.ID, blog_article.AUTHOR_ID, blog_article.CREATED_AT, blog_article.DELETED_AT FROM blog_article // WHERE blog_article.ID=1 AND blog_article.DELETED_AT IS NULL $article->delete(); // UPDATE blog_article SET DELETED_AT = '2006-10-21 10:58:56' WHERE blog_article.ID=1 $article->forceDelete(); // DELETE FROM blog_article WHERE blog_article.ID=1 $articles = ArticlePeer::doCount(new Criteria()); // SELECT COUNT(blog_article.ID) FROM blog_article WHERE blog_article.DELETED_AT IS NULL
You can also disable the behavior with:
sfPropelParanoidBehavior::disable();
The paranoid behavior will be disabled for the very next request.
$article->delete(); // UPDATE blog_article SET DELETED_AT = '2006-10-21 10:58:56' WHERE blog_article.ID=1 sfPropelParanoidBehavior::disable(); $article->delete(); // DELETE FROM blog_article WHERE blog_article.ID=1 $article->delete(); // UPDATE blog_article SET DELETED_AT = '2006-10-21 10:58:56' WHERE blog_article.ID=1
Additional Installation Notes
1) If you are not using the trunk (i.e. if you are using any of the 1.0.x bugfix releases) download this file and replace the corresponding one in your symfony library: http://trac.symfony-project.com/browser/trunk/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php
If you don't do this you will find that the behavior properly sets 'deleted_at' values, but fails to filter select results to exclude records marked deleted.
2) The plug-in does not modify your schema, so you need to add 'deleted_at' fields for each table you wish to enable this behavior for.
deleted_at: timestamp
3) As per ticket:1229, placing your sfPropelBehavior::add() after a class declaration (as the README advises) fails to work. Placing these in your app/config/config.php works.
/app/config/config.php
...
sfPropelBehavior::add('Address', array('paranoid'));
...
This configuration was tested with symfony 1.0.4.
As at 27-March-2008: Tested with Symfony 1.0.11: 1. Don't edit /app/config/config.php. Instead install the most recent file from http://trac.symfony-project.com/browser/trunk/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php
as discussed above, and rebuild your model with symfony propel-build-model. This will ensure results are filtered to exclude "paranoid deleted" entries.
Bugs still outstanding: Doesn't work with the form validator sfPropelUniqueValidator. The validation SQL with this validator doesn't specify WHERE DELETED_AT IS NULL so the record is still found even if its been "paranoid deleted".
Attachments
- sfPropelParanoidBehaviorPlugin-1.0.0.tgz (3.0 kB) - added by fabien on 10/21/06 11:18:17.
- sfPropelParanoidBehaviorPlugin-1.0.1.tgz (3.0 kB) -
updated for 1.0.0-beta1
, added by fred on 12/01/06 20:18:05. - sfPropelParanoidBehaviorPlugin-1.0.2.tgz (3.0 kB) - added by fabien on 02/28/08 08:55:44.