Blameable behavior does not work on Symfony2

1

I'm implementing the Blameable behavior in Symfony2 using Gedmo\Mapping\Annotation . I was able to successfully implement Timestampable , but I can not implement Blameable . The problem in question is that I can not get the user. I tried to rewrite the listener method that brings the Gedmo , but it can not access the user.

Regarding my config :

extension.listener:
    class: Basepoint\SruBundle\Listener\DoctrineExtensionListener
    calls:
        - [ setContainer, [ @service_container ] ]
    tags:
        # translatable sets locale after router processing
        - { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
        # loggable hooks user username if one is in security context
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

gedmo.listener.tree:
    class: Gedmo\Tree\TreeListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

gedmo.listener.translatable:
    class: Gedmo\Translatable\TranslatableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
        - [ setDefaultLocale, [ %locale% ] ]
        - [ setTranslationFallback, [ false ] ]        

gedmo.listener.timestampable:
    class: Gedmo\Timestampable\TimestampableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

gedmo.listener.sluggable:
    class: Gedmo\Sluggable\SluggableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

gedmo.listener.sortable:
    class: Gedmo\Sortable\SortableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

gedmo.listener.loggable:
    class: Gedmo\Loggable\LoggableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.blameable:
    class: Gedmo\Blameable\BlameableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

Class :

use Gedmo\Mapping\Annotation as Gedmo; // gedmo annotations
use Doctrine\ORM\Mapping as ORM;

// ...

    /**
     * @var string $createdBy
     * 
     * @Gedmo\Blameable(on="create")
     * @ORM\Column(type="string")
     */
    private $createdBy;

    /**
     * @var string $updatedBy
     *
     * @Gedmo\Blameable(on="update")
     * @ORM\Column(type="string")
     */
    private $updatedBy;

// ...

Timestampable is working correctly.

What can be my problem?

    
asked by anonymous 17.03.2014 / 11:10

1 answer

1

Description: DoctrineExtensions Listener

Add to DoctrineExtensionListener :

file: src / Xxxx / XxxBundle / Listener / DoctrineExtensionListener.php

namespace Xxxx\XxxBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DoctrineExtensionListener implements ContainerAwareInterface
{
...
   public function onLateKernelRequest(GetResponseEvent $event)
   {
...
   } 

   public function onKernelRequest(GetResponseEvent $event)
   {
        $securityContext = $this->container->get('security.context', ContainerInterface::NULL_ON_INVALID_REFERENCE);
        if (null !== $securityContext && null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            $blameable = $this->container->get('gedmo.listener.blameable');
            $blameable->setUserValue($securityContext->getToken()->getUser());
        }
    ...
    }
}
    
25.04.2014 / 06:35