Happyr/Doctrine-Specification

Delete entities by specification

peter-gribanov opened this issue · 2 comments

It would be nice to have a method for deleting entities according to the specification.

$rep->delete(Spec::in('id', $ids));
$rep->delete(Spec::orX(
    Spec::eq('enabled', false),
    Spec::lt('create_at', new \DateTime('-3 month'))
));

Simple implementation

public function delete(Specification $spec)
{
    $qb = $this->createQueryBuilder($this->alias);
    $this->applySpecification($qb, $specification);
    $qb->delete();

    return (bool) $qb->getQuery()->execute();
}

We will not do the short method, but the problem can be solved through the QueryBuilder:

$rep
    ->getQueryBuilder($spec)
    ->delete()
    ->getQuery()
    ->execute()
;