Created
April 29, 2025 21:30
-
-
Save michitheonlyone/8d666c749ca6b06e9585396c1e098302 to your computer and use it in GitHub Desktop.
Symfony deactivate or disable SoftDeletableFilter if referenced entity gets deleted will get you an 500 error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... controller Logic use EntityManagerInterface | |
$filters = $entityManager->getFilters(); | |
$filters->disable('softdeleteable'); | |
// .. get all the needed entitys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EntityWithNullingTheReference { | |
public function removeReferenceEntry(CashbookEntry $cashbookEntry): static | |
{ | |
if ($this->referenceEntries->removeElement($referenceEntry)) { | |
// set the owning side to null (unless already changed) | |
if ($referenceEntry->getReference() === $this) { | |
$referenceEntry->setReference(null); | |
} | |
} | |
return $this; | |
} | |
/** | |
* use the following methods in you're controller to clean the references to NULL | |
* $project->resetAllReferenceEntries(); | |
* $entityManager->remove($project); | |
* $entityManager->flush(); | |
*/ | |
public function resetAllReferenceEntries(): static | |
{ | |
foreach ($this->referenceEntries as $referenceEntry) { | |
$this->removeReferenceEntry($referenceEntry); | |
} | |
return $this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment