Skip to content

Commit

Permalink
Automatic update
Browse files Browse the repository at this point in the history
  • Loading branch information
xosofox committed May 24, 2011
1 parent a5f7391 commit 3686e2d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
30 changes: 21 additions & 9 deletions generator/lib/behavior/SoftDeleteBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* And an additional condition for every read query to only consider rows with no deletion date
*
* @author François Zaninotto
* @version $Revision: 2169 $
* @version $Revision: 2294 $
* @package propel.generator.behavior
*/
class SoftDeleteBehavior extends Behavior
Expand Down Expand Up @@ -52,14 +52,20 @@ public function objectMethods($builder)

public function addObjectForceDelete(&$script)
{
$peerClassName = $this->getTable()->getPhpName() . 'Peer';
$script .= "
/**
* Bypass the soft_delete behavior and force a hard delete of the current object
*/
public function forceDelete(PropelPDO \$con = null)
{
{$this->getTable()->getPhpName()}Peer::disableSoftDelete();
if(\$isSoftDeleteEnabled = {$peerClassName}::isSoftDeleteEnabled()) {
{$peerClassName}::disableSoftDelete();
}
\$this->delete(\$con);
if (\$isSoftDeleteEnabled) {
{$peerClassName}::enableSoftDelete();
}
}
";
}
Expand Down Expand Up @@ -346,32 +352,38 @@ public function addPeerDoSoftDelete(&$script)
*/
public static function doSoftDelete(\$values, PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection({$this->getTable()->getPhpName()}Peer::DATABASE_NAME, Propel::CONNECTION_WRITE);
}
if (\$values instanceof Criteria) {
// rename for clarity
\$criteria = clone \$values;
\$selectCriteria = clone \$values;
} elseif (\$values instanceof {$this->getTable()->getPhpName()}) {
// create criteria based on pk values
\$criteria = \$values->buildPkeyCriteria();
\$selectCriteria = \$values->buildPkeyCriteria();
} else {
// it must be the primary key
\$criteria = new Criteria(self::DATABASE_NAME);";
\$selectCriteria = new Criteria(self::DATABASE_NAME);";
$pks = $this->getTable()->getPrimaryKey();
if (count($pks)>1) {
$i = 0;
foreach ($pks as $col) {
$script .= "
\$criteria->add({$col->getConstantName()}, \$values[$i], Criteria::EQUAL);";
\$selectCriteria->add({$col->getConstantName()}, \$values[$i], Criteria::EQUAL);";
$i++;
}
} else {
$col = $pks[0];
$script .= "
\$criteria->add({$col->getConstantName()}, (array) \$values, Criteria::IN);";
\$selectCriteria->add({$col->getConstantName()}, (array) \$values, Criteria::IN);";
}
$script .= "
}
\$criteria->add({$this->getColumnForParameter('deleted_column')->getConstantName()}, time());
return {$this->getTable()->getPhpName()}Peer::doUpdate(\$criteria, \$con);
// Set the correct dbName
\$selectCriteria->setDbName({$this->getTable()->getPhpName()}Peer::DATABASE_NAME);
\$updateCriteria = new Criteria(self::DATABASE_NAME);
\$updateCriteria->add({$this->getColumnForParameter('deleted_column')->getConstantName()}, time());
return {$this->builder->getBasePeerClassname()}::doUpdate(\$selectCriteria, \$updateCriteria, \$con);
}
";
}
Expand Down
35 changes: 33 additions & 2 deletions test/testsuite/generator/behavior/SoftDeleteBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* $Id: SoftDeleteBehaviorTest.php 2168 2011-01-20 15:07:57Z francois $
* $Id: SoftDeleteBehaviorTest.php 2293 2011-05-23 19:40:33Z francois $
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -15,7 +15,7 @@
* Tests for SoftDeleteBehavior class
*
* @author François Zaninotto
* @version $Revision: 2168 $
* @version $Revision: 2293 $
* @package generator.behavior
*/
class SoftDeleteBehaviorTest extends BookstoreTestBase
Expand Down Expand Up @@ -220,6 +220,37 @@ public function testForceDelete()
$this->assertEquals(0, Table4Peer::doCount(new Criteria), 'forced deleted rows are not present in the database');
}

public function testForceDeleteDoesNotDisableSoftDelete()
{
$t1 = new Table4();
$t1->save();
$t2 = new Table4();
$t2->save();
$t1->forceDelete();
$t2->delete();
$this->assertTrue($t1->isDeleted(), 'forceDelete() actually deletes a row');
$this->assertFalse($t2->isDeleted(), 'forceDelete() does not affect further delete() calls');
Table4Peer::disableSoftDelete();
$this->assertEquals(1, Table4Peer::doCount(new Criteria), 'forced deleted rows are not present in the database');
}

public function testForceDeleteReEnablesSoftDelete()
{
$t = new Table4();
$t->save();
$t->forceDelete();
$this->assertTrue(Table4Peer::isSoftDeleteEnabled(), 'forceDelete() reenables soft delete');
}

public function testForceDeleteDoesNotReEnableSoftDeleteIfDisabled()
{
Table4Peer::disableSoftDelete();
$t = new Table4();
$t->save();
$t->forceDelete();
$this->assertFalse(Table4Peer::isSoftDeleteEnabled(), 'forceDelete() does not reenable soft delete if previously disabled');
}

public function testQueryIncludeDeleted()
{
$t = new Table4();
Expand Down
8 changes: 6 additions & 2 deletions test/tools/helpers/bookstore/BookstoreDataPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ public static function depopulate($con = null)
);
// free the memory from existing objects
foreach ($peerClasses as $peerClass) {
foreach ($peerClass::$instances as $o) {
// $peerClass::$instances crashes on PHP 5.2, see http://www.propelorm.org/ticket/1388
$r = new ReflectionClass($peerClass);
$p = $r->getProperty('instances');
foreach ($p->getValue() as $o) {
$o->clearAllReferences();
}
}
Expand All @@ -257,7 +260,8 @@ public static function depopulate($con = null)
}
$con->beginTransaction();
foreach ($peerClasses as $peerClass) {
$peerClass::doDeleteAll($con);
// $peerClass::doDeleteAll() crashes on PHP 5.2, see http://www.propelorm.org/ticket/1388
call_user_func(array($peerClass, 'doDeleteAll'), $con);
}
$con->commit();
}
Expand Down

0 comments on commit 3686e2d

Please sign in to comment.