Wickethewok
Wickethewok

Reputation: 6720

PHP Doctrine SoftDelete - Include deleted records?

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?

Upvotes: 7

Views: 7117

Answers (5)

rhaps107
rhaps107

Reputation: 41

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');

Upvotes: 4

unlexx
unlexx

Reputation: 21

$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.

Upvotes: 2

factorlabs
factorlabs

Reputation: 41

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();
}

Upvotes: 4

Daniel Pozzi
Daniel Pozzi

Reputation: 11

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){}
}

Upvotes: 1

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 :) )

Upvotes: 2

Related Questions