我會檢查你的的setName功能實際上是做一些事情($這個 - >名稱= $名字......)的第一件事情,如果它已經工作,那麼你可以在你的服務定義事件監聽器.yml在調用flush時觸發。
entity.listener:
class: YourName\YourBundle\EventListener\EntityListener
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: doctrine.event_listener, event: onFlush }
然後你定義EntityListener
namespace YourName\YourBundle\EventListener;
use Doctrine\ORM\Event;
use Symfony\Component\DependencyInjection\ContainerAware;
class EntityListener extends ContainerAware
{
/**
* Gets all the entities to flush
*
* @param Event\OnFlushEventArgs $eventArgs Event args
*/
public function onFlush(Event\OnFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
//Insertions
foreach ($uow->getScheduledEntityInsertions() as $entity) {
# your code here for the inserted entities
}
//Updates
foreach ($uow->getScheduledEntityUpdates() as $entity) {
# your code here for the updated entities
}
//Deletions
foreach ($uow->getScheduledEntityDeletions() as $entity) {
# your code here for the deleted entities
}
}
}
如果您需要知道哪些實體被改變,但他們一直保存到數據庫後,做一些與他們,只存儲實體在私有數組中更改,然後定義一個從數組中獲取實體的onFlush事件。
順便說一句,要觸發這種事件,您需要在實體上添加@ORM \ HasLifecycleCallbacks。
當您發現實體發生了變化時,您想完成什麼? –
如果您查看'UnitOfWork'的源代碼,在'isScheduledForUpdate'的註釋中,他們會說「注意:目前不太有用,因爲髒實體只在提交時註冊」 –
呃...我需要發送一個當產品發生變化時向系統用戶發出通知,但是當產品數據沒有改變時我應該忽略通知(我將表單的POST數據綁定到實體以使用該Doctrine易用性後,我仍然會進行刷新)。我會嘗試之前執行UnitOfWork :: computeChangeSets(),可能會降低性能,但可以工作。 – eagleoneraptor