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;該表有兩個成員,都與PlaceInfo類有關。并且PlaceInfo類沒有任何與DistCache類相關的成員。然后我想 delete the DistCache entry when one of two members(placeOne or placeTwo) is deleted我四處搜索并發現cascade="remove" or orphanRemoval=true,但兩者看起來有點不同我的目的。我該如何解決?
1 回答

BIG陽
TA貢獻1859條經驗 獲得超6個贊
我可以看到對于您設置的兩個 PlaceInfo 對象nullable=false,因此在刪除 PlaceInfo 時,不僅要刪除 entityManager 管理的 DistCache 實體,還必須刪除數據庫中的實體。
我建議您可以使用Doctrine 生命周期回調中的preRemove事件。在 PlaceInfo 記錄的刪除事件中,您查詢所有使用已刪除 PlaceInfo 對象的 DistCache 對象并首先刪除它們。
簡而言之,您需要:
在您的課程之前添加 @ORM\HasLifecycleCallbacks 以啟用生命周期。
在 PlaceInfo 類中添加 preRemove 函數:
/**
* @ORM\PreRemove
* @param LifecycleEventArgs $event
*/
public function removeDistCache(LifecycleEventArgs $event)
{
$em = $event->getEntityManager();
// Use $em to query and delete the DistCache entities
}
- 1 回答
- 0 關注
- 164 瀏覽
添加回答
舉報
0/150
提交
取消