Gedmo Timestampable注释似乎不适用于事件监听器

时间:2013-06-21 16:07:37

标签: php symfony doctrine-orm

所以,

我有一个使用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();
        }
    }
}

当我删除事件监听器时,一切都很好,并且正确填写了时间戳字段。但是,当我在启用侦听器的情况下创建新实体时,未填写时间戳字段。

那么,我的问题是,什么会导致事件监听器阻止Gedmo填写时间戳字段?我可能正在做一些非常愚蠢的事情,但到目前为止我看不出那可能是什么......

1 个答案:

答案 0 :(得分: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作为构造参数传递。当你想到它时,这实际上有点意义,并且并没有真正阻碍我做的事情,因为LifecycleEventArgs对象和PostFlushEventArgs对象中的getEntityManager事件可用