6

If I have one of my PHP Doctrine objects act as a SoftDelete, is it possible to include deleted items in the results of certain queries? What I'm looking for is something like this...

$q = Doctrine_Query::create()
    ->select('*')
    ->from('Test t')
    ->where('id < ?', 25)
    *->includeDeleted()*;

Something like this would be useful as for most queries I want deleted records excluded, but sometimes (for example, to administrators) I want to be able to include records that have been soft deleted. Is there some good way to do this with SoftDelete or should I simply add an additional where clause onto most queries?

j0k
  • 22,600
  • 28
  • 79
  • 90
Wickethewok
  • 6,534
  • 11
  • 42
  • 40

5 Answers5

4

Looks like you may disable soft-deleteable filter http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html

// This will disable the SoftDeleteable filter, so entities which were "soft-deleted" will appear in results $em->getFilters()->disable('soft-deleteable');

rhaps107
  • 41
  • 1
4

It returns all records from table (include softDeleted)

public function findAllWithDeleted()
{
    $query = $this->createQuery('a');
    $query->addWhere('(a.deleted_at IS NULL OR a.deleted_at IS NOT NULL)');
    return $query->execute();
}
factorlabs
  • 41
  • 3
2

From a cursory glance at the source here and here, this functionality does not seem to provided by the SoftDelete behaviour. You have to add a where clause manually like you suggest.

(I may be wrong, but I'm pretty sure Doctrine_Query can not be extended dynamically by way of a behaviour like you can with Doctrine_Record. You could always write a special Doctrine_Query subclass which adds includeDeleted(), but that seems like more thouble than it's worth :) )

2
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);

After this you can select, update, delete data like softDelete is off.

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
unlexx
  • 21
  • 1
1

We had a bug in Doctrine 1.2.2 where ATTR_USE_DQL_CALLBACKS was ignored AND the softdelete column had the wrong name. The solution is to overwrite the listener:

$m = new MyModel;
$m->setListener(new MockListener());
$m->getTable()->getQueryObject(); //... and so forth

with a dummy class:

class MockListener implements Doctrine_Overloadable
{
    public function __call($m, $a){}
}
Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153