title: Doctrine2 dynamic table name for entity
body: <p>I'm trying to add a dynamic table name for some of my entities.</p>
  
  <p>I read this post [Dynamic Table/Entity names in Doctrine 2][1]
    [1]: <a href="https://stackoverflow.com/questions/19622604/dynamic-table-entity-names-in-doctrine-2">Dynamic Table/Entity names in Doctrine 2</a>   and this one about doctrine listener [doctrine listener][2] [2] : <a href="http://doctrine-orm.readthedocs.org/en/latest/reference/events.html" rel="noreferrer">http://doctrine-orm.readthedocs.org/en/latest/reference/events.html</a></p>
  
  <p>So here is the way I try to do it :</p>
  
  <p>I've create an entity with a wildcard in it's table name :</p>
  
  <pre><code>&lt;?php
  
  namespace DD\MyBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * SiteElec
   *
   * @ORM\Table(name="site_x_elec")
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks
   * @ORM\EntityListeners({"DD\MyBundle\Entity\Listener\SiteElecListener"})
   */
  class SiteElec
  {
      I skip properties with getters and setters
  
  }
  </code></pre>
  
  <p>Next I've done a listener for this entity : </p>
  
  <pre><code>&lt;?php
  
  namespace DD\MyBundle\Entity\Listener;
  
  use DD\UserBundle\Entit\User;
  use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  
  /**
   * Listener pour l'entité siteElec.
   */
  class SiteElecListener {
  
  
  private $_tableName = null;
  protected $token_storage;
  protected $user;
  
  public function getTableName() {
      return $this-&gt;_tableName;
  }
  
  public function setTableName($tableName) {
      $this-&gt;_tableName = $tableName;
      return $this;
  }
  
  public function __construct(TokenStorageInterface $token_storage) {
  
      $this-&gt;token_storage = $token_storage;
  
      if($token_storage-&gt;getToken() != null){
  
      }
  }
  
  /*
   * @ORM\LoadClassMetadata
   * @param LoadClassMetadataEventArgs $eventArgs
   */
  public function loadClassMetadata(LoadClassMetadataEventArgs $args) {
  
      //$this-&gt;user = $this-&gt;token_storage-&gt;getToken();
      //var_dump($this-&gt;token_storage);
  
      $classMetadata = $args-&gt;getClassMetadata();
      $table = $classMetadata-&gt;table;
          var_dump($classMetadata);
  //        $table['name'] = 'site_'.$this-&gt;user-&gt;getSite()-&gt;getId().'_elec';
  //        $classMetadata-&gt;setPrimaryTable($table);
  }
  
  }
  </code></pre>
  
  <p>Next I declare my listener in config file : </p>
  
  <pre><code>    dd.entity_listener.site_elec:
      class:      DD\MyBundle\Entity\Listener\SiteElecListener
      arguments:  
          - "@security.token_storage"
      tags:
          - { name: doctrine.event_listener, event: loadClassMetadata }
  </code></pre>
  
  <p>If I look at <strong>$classMetadata var</strong> I only have my user entity and the associated entity but not my SiteElec entity</p>
  
  <p>I must have done a mistake somewhere but I don't know where.</p>
  
  <p>Any idea ?
  Thanks</p>