2015-05-05 12 views
7

qualcuno può fornire un esempio completo dell'estensione Blameable Gedmo e, in particolare, la configurazione del Listener Blameable?configurare l'estensione doctrine biasimevole con symfony2

Sto usando il codice fornito da documentazione:

* @var User $createdBy 
* 
* @Gedmo\Blameable(on="create") 
* @ORM\ManyToOne(targetEntity="Cf\UserBundle\Entity\User") 
* @ORM\JoinColumn(name="createdBy", referencedColumnName="id") 
*/ 
private $createdBy; 

/** 
* @var User $updatedBy 
* 
* @Gedmo\Blameable(on="update") 
* @ORM\ManyToOne(targetEntity="Cf\UserBundle\Entity\User") 
* @ORM\JoinColumn(name="updatedBy", referencedColumnName="id") 
*/ 
private $updatedBy; 

ma le colonne del database CreatedBy e UpdatedBy sono sempre NULL.

La documentazione fornisce un esempio per configurare gli altri listener (ad esempio, timestamp che ho funzionato) ma non trovo esempi o documentazione per l'ascoltatore biasimevole.

Grazie per qualsiasi aiuto !!

========================================= =================

EDIT per rispondere Jean:

sì ho aggiunto l'uso che è:

use Gedmo\Mapping\Annotation as Gedmo; 

io uso anche il Timestampable con il tratto fornito:

use Gedmo\Timestampable\Traits\TimestampableEntity; 

// doctrine comments removed 
class Document 
{ 
    use TimestampableEntity; 
... 
} 

e le impostazioni di data e ora on è:

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

Timespambable funziona bene. Ho provato una configurazione simile per l'ascoltatore biasimevole in quanto ha un metodo setUserValue:

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

ma non funziona, ottengo questo errore (i 4 fasci sono quelli utilizzati nel mio progetto):

La classe 'Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage' non è stata trovata negli spazi dei nomi chain configurati Cf \ UserBundle \ Entity, Cf \ DocumentBundle \ Entity, Cf \ SouffleBundle \ Entity, FOS \ UserBundle \ Modello

Capisco che manchi l'ID utente o il token di sicurezza come argomento in una w ay or another, ma non riesco a trovare un esempio da nessuna parte. Ecco dove sono bloccato. Qualche idea ?

+0

hai messo il 'dell'uso al in cima alla vostra classe? Puoi mostrarcelo? – Jean

+0

Inoltre, Blameable funziona solo se l'entità viene creata \ aggiornata con il contesto di sicurezza e contiene un token utente, che viene preso in considerazione e mantenuto nel proprio campo. – Jean

+0

Usi [https://github.com/stof/StofDoctrineExtensionsBundle](https://github.com/stof/StofDoctrineExtensionsBundle)? Ce l'ho e le opere biasimevoli. – kba

risposta

10

Ho anche trovato difficile abilitare il comportamento di Blomeable con StofDoctrineExtensionsBundle (assumiamo che lo stiate usando).

C'è un pezzo di configurazione che non è menzionato in questo bundle:

# Add in your app/config/config.yml 
stof_doctrine_extensions: 
    orm: 
     default: 
      blameable: true 

A parte questo, ho creato un tratto BlameableEntity:

<?php 

namespace AppBundle\Entity\Traits; 

use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 
use AppBundle\Entity\User; 

/** 
* Add Blameable behavior to an entity. 
*/ 
trait BlameableEntity { 

    /** 
    * @var User 
    * 
    * @Gedmo\Blameable(on="create") 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumn(name="created_by", referencedColumnName="id") 
    */ 
    protected $createdBy; 

    /** 
    * @var User 
    * 
    * @Gedmo\Blameable(on="update") 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumn(name="updated_by", referencedColumnName="id") 
    */ 
    protected $updatedBy; 

    /** 
    * Set createdBy 
    * 
    * @param User $createdBy 
    * @return Object 
    */ 
    public function setCreatedBy(User $createdBy) 
    { 
     $this->createdBy = $createdBy; 

     return $this; 
    } 

    /** 
    * Get createdBy 
    * 
    * @return User 
    */ 
    public function getCreatedBy() 
    { 
     return $this->createdBy; 
    } 

    /** 
    * Set updatedBy 
    * 
    * @param User $updatedBy 
    * @return Object 
    */ 
    public function setUpdatedBy(User $updatedBy) 
    { 
     $this->updatedBy = $updatedBy; 

     return $this; 
    } 

    /** 
    * Get updatedBy 
    * 
    * @return User 
    */ 
    public function getUpdatedBy() 
    { 
     return $this->updatedBy; 
    } 

} 

E nella vostra entità, è sufficiente aggiungere un use dichiarazione come questa:

<?php 

namespace AppBundle\Entity; 

use AppBundle\Entity\Traits\BlameableEntity; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Your precious Foo entity 
* 
* @ORM\Table(name="foo") 
* @ORM\Entity(repositoryClass="AppBundle\Entity\FooRepository") 
*/ 
class Foo 
{ 
    use BlameableEntity; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    // ... 

Spero che questo aiuti!

2

il piccolo contributo aggiuntivo per le persone che attivano symfony 3.0 +:

In "doctrine_extensions.yml" aggiungere/modificare:

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

Se qualcuno ha ancora questo problema, posso finalmente ottenere Gedmo biasimevole lavorativi successivi alla seguente procedura:

  1. Seguire questo guida https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/symfony2.md
  2. Aggiungi questo al doctrine_extensions.yml

    gedmo.listener.blameable: 
        class: Gedmo\Blameable\BlameableListener 
        tags: 
        - { name: doctrine.event_subscriber, connection: default } 
        calls: 
        - [ setAnnotationReader, [ "@annotation_reader" ] ] 
    
  3. modificare il metodo onKernelRequest come segue:

    public function onKernelRequest(GetResponseEvent $event) { 
        if (Kernel::MAJOR_VERSION == 2 && Kernel::MINOR_VERSION < 6) { 
         $securityContext = $this->container->get('security.context', ContainerInterface::NULL_ON_INVALID_REFERENCE); 
         if (null !== $securityContext && null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
          $loggable = $this->container->get('gedmo.listener.loggable'); 
          $loggable->setUsername($securityContext->getToken()->getUsername()); 
         } 
        } 
        else { 
         $tokenStorage = $this->container->get('security.token_storage')->getToken(); 
         $authorizationChecker = $this->container->get('security.authorization_checker'); 
         if (null !== $tokenStorage && $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
          $loggable = $this->container->get('gedmo.listener.loggable'); 
          $loggable->setUsername($tokenStorage->getUser()); 
          $blameable = $this->container->get('gedmo.listener.blameable'); 
          $blameable->setUserValue($tokenStorage->getUser()); 
         } 
        } 
    }