2013-06-21 41 views
2

所以,Gedmo Timestampable註釋似乎並不工作,事件監聽器

我有一個實體的建立與Timestampable領域,像這樣:

<?php 
namespace Acme\Bundle\Entity; 

use Acme\PathEnumerableInterface as EnumerableInterface; 
use Acme\PathEnumerable as PathEnumerableTrait; 

use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* SomeEntity 
* 
* @ORM\Table() 
* @ORM\HasLifecycleCallbacks 
*/ 
class SomeEntity implements EnumerableInterface 
{ 
    use PathEnumerableTrait; 

    /** 
    * @var \DateTime 
    * 
    * @Gedmo\Timestampable(on="create") 
    * @ORM\Column(name="created_at", type="datetime") 
    */ 
    private $createdAt; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="updated_at", type="datetime") 
    * @Gedmo\Timestampable(on="update") 
    */ 
    private $updatedAt; 


    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 
} 

我已經然後建立一個生命週期訂戶postPersist,postUpdate和postFlush事件。該service.yml看起來是這樣的:

services: 
    acme.element_listener: 
     class: %iacme.element_listener.class% 
     arguments: 
      manager: "@doctrine.orm.default_entity_manager" 
     tags: 
      - { name: doctrine.event_subscriber, connection: default } 

實際聽衆看起來是這樣的:

<?php 

namespace Acme\Bundle\EventListener; 

use Acme\PathEnumerableInterface; 
use Doctrine\ORM\Event\LifecycleEventArgs; 
use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Event\PostFlushEventArgs; 

class EventListener 
{ 
    /** 
    * @var Doctrine\ORM\EntityManager 
    */ 
    private $entityManager; 

    /** 
    * @var array 
    */ 
    private $paths = []; 

    public function __construct(EntityManager $entityManager) 
    { 
     $this->entityManager = $entityManager; 
    } 

    public function getSubscribedEvents() 
    { 
     return ['postPersist']; 
    } 

    private function postPersist(LifecycleEventArgs $args) 
    { 
     $entity = $args->getEntity(); 
     // Set $newPath to a generated path and check so we don't end up in an infinite loop 
     if ($entity->getPath() != $newPath) { 
      //... Do some stuff with the entity 
      $this->entityManager->persist($entity); 
      $this->entityManager->flush(); 
     } 
    } 
} 

當我刪除了事件偵聽器,一切都很好,和timestampable字段填寫正確。但是,當我創建一個新的實體時,啓用了偵聽器的時間戳字段未被填寫。

我的問題是,那麼會導致事件監聽器停止Gedmo填充時間戳字段?我可能做一些非常愚蠢的,但至今我什麼也看不見,可能是......

回答

1

好了,有幾件事情錯在這裏,是造成我的問題:

  1. 我的EventListener類沒有實現EventSubscriber。類應該已被宣佈像這樣:

    <?php 
    
    namespace Acme\Bundle\EventListener; 
    
    use Acme\PathEnumerableInterface; 
    use Doctrine\ORM\Event\LifecycleEventArgs; 
    use Doctrine\ORM\EntityManager; 
    use Doctrine\ORM\Event\PostFlushEventArgs; 
    use Doctrine\Common\EventSubscriber; 
    
    class EventListener implements EventSubscriber 
    { 
        // ..... 
    } 
    
  2. 您無法通過EntityManager的作爲結構參數。當您考慮這一點時,這實際上具有相當多的意義,並且不會妨礙我做什麼,因爲getEntityManager事件在LifecycleEventArgs對象和PostFlushEventArgs對象中可用。