![](/img/trans.png)
[英]Doctrine symfony remove entity with OneToMany - ManyToOne relationship
[英]Symfony general entity relationship in Doctrine
我需要實現一個PendingMessage類來存儲Symfony2應用程序的某些實體的通知。 有時會為一個實體創建通知,有時會為另一個實體(有很多實體)創建通知。
有什么方法可以在此PendingMessage類中進行ORM關系存儲一個實體,但不存儲特定的實體類型,而只存儲一個普通的實體(類),以便具有稱為“ $ destination”的屬性,該屬性應為實體類型。
我應該實現一個接口嗎? 歡迎任何幫助!
你可以添加2個參數到您的PendingMessage實體,一個entityName
,其他entityID
。
使用這些參數,您可以訪問控制器中的存儲庫,例如
$em = $this->getDoctrine()->getManager();
$pendingMessage = $this->getRepository('youBundle:PendingMessage')->find(1234);
$targetEntity = $this->getRepository('yourBundle:'.$pendingMessage->getEntityName())->find($pendingMessage->getEntityID());
如果您想對每個實體使用此PendingMessage進行相同的操作,我將編寫一個接口,供您將要使用的每個存儲庫使用,以確保在您要使用的每個存儲庫中都提供此功能動態接收。
如果這不是您想要的,請澄清您的問題。
您可以在PendingMessage實體中添加字段,您將在其中存儲為此消息創建的序列化實體。
然后,如果您想更改此特定實體,則可以執行以下操作:
$pendingMessage = $this->getRepository('Bundle:PendingMessage')->find(1);
$detachedEntity = $pendingMessage->getDestination();
$entity = $em->merge($detachedEntity);
$entity->anyChangesYouWant();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.