Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Gedmo时间标签注释不';似乎无法使用事件侦听器_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php Gedmo时间标签注释不';似乎无法使用事件侦听器

Php Gedmo时间标签注释不';似乎无法使用事件侦听器,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,所以 我有一个实体设置了可时间戳字段,如下所示: <?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; /** *

所以

我有一个实体设置了可时间戳字段,如下所示:

<?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;
    }
}
实际的侦听器如下所示:

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();
        }
    }
}

好的,这里有两个错误导致了我的问题:

  • 我的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
    {
        // .....
    }