option to move entries from one entity to another upon deletion (#409)

This commit is contained in:
Kevin Papst
2018-11-12 00:01:49 +01:00
committed by GitHub
parent 1dabeedaa0
commit f9dc42d1f9
18 changed files with 524 additions and 23 deletions

View File

@@ -15,6 +15,7 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Model\CustomerStatistic;
use App\Repository\Query\CustomerQuery;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Pagerfanta;
@@ -121,6 +122,43 @@ class CustomerRepository extends AbstractRepository
$qb->andWhere('c.visible = 0');
}
if (!empty($query->getIgnoredEntities())) {
$qb->andWhere('c.id NOT IN(:ignored)');
$qb->setParameter('ignored', $query->getIgnoredEntities());
}
return $this->getBaseQueryResult($qb, $query);
}
/**
* @param Customer $delete
* @param Customer|null $replace
* @throws \Doctrine\ORM\ORMException
*/
public function deleteCustomer(Customer $delete, ?Customer $replace = null)
{
$em = $this->getEntityManager();
$em->beginTransaction();
try {
if (null !== $replace) {
$qb = $em->createQueryBuilder();
$qb
->update(Project::class, 'p')
->set('p.customer', ':replace')
->where('p.customer = :delete')
->setParameter('delete', $delete)
->setParameter('replace', $replace)
->getQuery()
->execute();
}
$em->remove($delete);
$em->flush();
$em->commit();
} catch (ORMException $ex) {
$em->rollback();
throw $ex;
}
}
}