Blameable behavior does not work on Symfony2

Asked

Viewed 108 times

1

I am implementing in Symfony2 the behavior Blameable using the Gedmo\Mapping\Annotation. I have successfully implemented the Timestampable, but I cannot implement the Blameable. The problem is that I can’t get the user. I tried to rewrite the listener that brings the Gedmo, but he can’t access the user.

In relation to 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;

// ...

The Timestampable is working correctly.

What could be my problem?

  • Are you currently logged in with a user? From what I saw in the documentation, Doctrine itself already automatically tries to assign a user to the $createdBy and $updatedBy attributes, regardless of whether the attribute is a string or a reference.

1 answer

1


Description: Doctrineextensions Listener

Agregar en 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());
        }
    ...
    }
}
  • Hi Javier! Welcome to Stackoverflow in English. Can you translate the question to EN please? Spanish is off-topic here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.