In the definition, you cannot delete the record pointed to by the foreign key without setting the key to null ( onDelete="SET NULL" ) or cascading the delete operation ( there are two options - ORM level: cascade={"remove"} | base level data: onDelete="CASCADE" ).
There is an alternative to setting a default value for an existing record , but you have to do it manually, I don’t think that Doctrine supports this out-of-the-box (please correct me if I am wrong, but in this case setting a default not required).
This rigor reflects the concept of foreign key constraints; as @ Théo said:
a FK - ensure data consistency.
Soft deletion (already mentioned) is one solution, but you can also add an extra removed_page_id column that you synchronize with page_id just before you delete it in the preRemove event preRemove (Call back life cycle). Whether such information is of any value, but I believe that you have some significance for this, otherwise you would not ask this question.
I definitely do not claim that this is good practice , but it is at least what you can use for your case with the edge. So something on the line:
In your Revision :
/** * @ORM\ManyToOne(targetEntity="Page", cascade="persist") * @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="SET NULL") */ private $parentPage; /** * @var int * @ORM\Column(type="integer", name="removed_page_id", nullable=true) */ protected $removedPageId;
And then in your Page :
public function preRemovePageHandler(LifecycleEventArgs $args) { $entityManager = $args->getEntityManager(); $page = $args->getEntity(); $revisions = $page->getRevisions(); foreach($revisions as $revision){ $revision->setRemovedPageId($page->getId()); $entityManager->persist($revision); } $entityManager->flush(); }
Alternatively, you could, of course, set the correct value to $removedPageId during the build of your Revision , then you don’t even have to perform a lifecycle callback on deletion.
Wilt
source share