Reputation: 12423
class DistCache
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PlaceInfo")
* @ORM\JoinColumn(nullable=false)
*/
private $placeOne;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PlaceInfo")
* @ORM\JoinColumn(nullable=false)
*/
private $placeTwo;
This table has two members, both related to PlaceInfo
class.
And PlaceInfo
class doesn't have any member related to DistCache
class.
Then I want to delete the DistCache entry when one of two members(placeOne or placeTwo) is deleted
I googled around and found cascade="remove" or orphanRemoval=true
, but both looks bit different my purpose.
How can I solve?
Upvotes: 1
Views: 61
Reputation: 294
I can see that for both PlaceInfo object you set nullable=false
, so when deleting a PlaceInfo, not only have to delete the DistCache entities managed by entityManager, you have to delete the ones in the database too.
I suggest you can use preRemove
event from Doctrine life cycle callbacks.
On the remove event of a PlaceInfo record, you query all the DistCache objects which use the deleted PlaceInfo object and remove them first.
In short, you need to :
Add @ORM\HasLifecycleCallbacks before your class to enable life cycles.
Add preRemove function in PlaceInfo class :
/**
* @ORM\PreRemove
* @param LifecycleEventArgs $event
*/
public function removeDistCache(LifecycleEventArgs $event)
{
$em = $event->getEntityManager();
// Use $em to query and delete the DistCache entities
}
Upvotes: 1