我讀:http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html有沒有辦法指定一個Doctrine事件監聽器的實體?
看來,使用Doctrine事件偵聽器這種方式是有點浪費,因爲我不能只定義一個特定的實體來聽,並有一個監聽器類中檢查實體類。這似乎是一種浪費。有沒有辦法指出特定的實體來聽?
我讀:http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html有沒有辦法指定一個Doctrine事件監聽器的實體?
看來,使用Doctrine事件偵聽器這種方式是有點浪費,因爲我不能只定義一個特定的實體來聽,並有一個監聽器類中檢查實體類。這似乎是一種浪費。有沒有辦法指出特定的實體來聽?
不需要。您需要將此邏輯包含在偵聽器的頂部。這是典型的instanceof
檢查:
public function prePersist($eventArgs)
{
// i.e. using the MongoDB ODM
$doc = $eventArgs->getDocument();
if (!$doc instanceof MyModel) {
return;
}
}
由於學說2.4,你可以使用Entity listeners,他們被稱爲僅針對特定的實體類型。
這似乎是一種浪費,不是嗎?因爲當我只需要一個實體時,它就會在每個實體上被調用。 – DavidW 2012-01-02 15:40:12
增加這種上游邏輯只會增加複雜性,而不會帶來真正的性能增益。 – 2012-01-02 15:45:48
好的,謝謝克里斯。 – DavidW 2012-01-02 15:52:27