2016-08-18 36 views
0

我需要實現一個PendingMessage類來存儲Symfony2應用程序的一些實體的通知。有時會爲一個實體創建通知,有時會爲另一個實體創建通知(有許多實體)。Symfony在Doctrine中的一般實體關係

有沒有辦法在這個PendingMessage類中做一個ORM關係來存儲一個實體,但不是一個特定的實體類型,只是一個普通的實體(類),爲了有一個名爲'$ destination'的屬性應該是一個實體類型。

我應該實現一個接口嗎?歡迎任何幫助!

回答

1

您可以將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每一個實體可以做同樣的操作,我會寫將要使用的接口通過您將使用的每個存儲庫,可以確保在您將動態接收的每個存儲庫中提供此函數。

如果這不是你想要的,請澄清你的問題。

+0

是的,我想這就是我要找的......但我不知道該怎麼實現這個...這兩個參數的接口? – jmunozco

1

您可以在PendingMessage實體中添加字段,您將在其中存儲爲其創建此消息的序列化實體。

然後如果你想改變這個特殊的實體,你會做這樣的事情:

$pendingMessage = $this->getRepository('Bundle:PendingMessage')->find(1); 
$detachedEntity = $pendingMessage->getDestination(); 
$entity = $em->merge($detachedEntity); 
$entity->anyChangesYouWant(); 
+0

如何存儲這個序列化的實體? – jmunozco

+0

作爲文本:)在谷歌查找它。有很多例子。 – mmmm

相關問題