To implement soft deletes in Doctrine with Symfony, you can follow these steps:
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deletedAt;
public function findNotDeleted()
{
$qb = $this->createQueryBuilder('e');
$qb->andWhere('e.deletedAt IS NULL');
return $qb->getQuery()->getResult();
}
remove
method in your controller to set the "deletedAt" field when an entity is soft deleted instead of actually removing it from the database.public function remove(EntityManager $em, YourEntity $entity)
{
$entity->setDeletedAt(new \DateTime());
$em->persist($entity);
$em->flush();
}
$entities = $repository->findNotDeleted();
By following these steps, you can easily implement soft deletes in Doctrine with Symfony, preserving data integrity and allowing for the possibility of restoring soft deleted entities in the future if needed.