Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 dynamic table name for entity

I'm trying to add a dynamic table name for some of my entities.

I read this post [Dynamic Table/Entity names in Doctrine 2][1] [1]: Dynamic Table/Entity names in Doctrine 2 and this one about doctrine listener [doctrine listener][2] [2] : http://doctrine-orm.readthedocs.org/en/latest/reference/events.html

So here is the way I try to do it :

I've create an entity with a wildcard in it's table name :

<?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

}

Next I've done a listener for this entity :

<?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->_tableName;
}

public function setTableName($tableName) {
    $this->_tableName = $tableName;
    return $this;
}

public function __construct(TokenStorageInterface $token_storage) {

    $this->token_storage = $token_storage;

    if($token_storage->getToken() != null){

    }
}

/*
 * @ORM\LoadClassMetadata
 * @param LoadClassMetadataEventArgs $eventArgs
 */
public function loadClassMetadata(LoadClassMetadataEventArgs $args) {

    //$this->user = $this->token_storage->getToken();
    //var_dump($this->token_storage);

    $classMetadata = $args->getClassMetadata();
    $table = $classMetadata->table;
        var_dump($classMetadata);
//        $table['name'] = 'site_'.$this->user->getSite()->getId().'_elec';
//        $classMetadata->setPrimaryTable($table);
}

}

Next I declare my listener in config file :

    dd.entity_listener.site_elec:
    class:      DD\MyBundle\Entity\Listener\SiteElecListener
    arguments:  
        - "@security.token_storage"
    tags:
        - { name: doctrine.event_listener, event: loadClassMetadata }

If I look at $classMetadata var I only have my user entity and the associated entity but not my SiteElec entity

I must have done a mistake somewhere but I don't know where.

Any idea ? Thanks

like image 539
bedomon Avatar asked Jun 16 '15 15:06

bedomon


1 Answers

You can use a dynamic table name for some of entities in Doctrine 2 in this way

1) Set Table(name="NULL") in entity

<?php

namespace DD\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * SiteElec
 *
 * @Table(name="NULL")
 * @ORM\Entity
 */
class SiteElec { ... }

2) Then set table name before using entity

$EM
  ->getClassMetadata('DD\MyBundle\Entity\SiteElec')
  ->setTableName($DYNAMIC_TABLE_NAME);

3) Use entity with dynamic table name as usual

$result = $EM
  ->getRepository('DD\MyBundle\Entity\SiteElec')
  ->findBy(['id' => $SOME_ID]);
like image 169
Sasha Kos Avatar answered Sep 28 '22 09:09

Sasha Kos