user3174311
user3174311

Reputation: 2003

How to programmatically change table prefix in symfony2-doctrine

I setup my symfony3 application to use 2 different databases. they are pretty much similar, structure of tables are the same and so the fields. The problem is, for example, the article table in db1 is called db1_article and article table in db2 is called db2_article. They have different data but same structure.

Now, I am setting up an entity for articles like that:

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

I'd prefer not to create a different entity for the same table in db2, can I dinamically define the table name somewhere in order to avoid duplications?

thanks

Upvotes: 1

Views: 1143

Answers (2)

felipep
felipep

Reputation: 2512

I would go for an approach using different entity managers for each database, so you can use the same entities.

//config.yml
doctrine:
    dbal:
        default_connection: first_entity_manager
        connections:
            first_entity_manager:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            second_entity_manager:
                driver:   %database_2nd_driver%
                host:     %database_2nd_host%
                port:     %database_2nd_port%
                dbname:   %database_2nd_name%
                user:     %database_2nd_user%
                password: %database_2nd_password%
                charset:  UTF8
    orm:
        default_entity_manager: first_entity_manager
        entity_managers:
            first_entity_manager:
                connection:     first_entity_manager
                mappings:
                    AppBundle: ~
            second_entity_manager:
                connection:     second_entity_manager
                mappings:
                    AppBundle: ~

Then just program some functions to use the correct entity manager

$em_first = $this->getDoctrine()->getManager('first_entity_manager');
$em_second = $this->getDoctrine()->getManager('second_entity_manager');

$article_first_em = $em_first->getRepository('AppBundle:Article')->find(1);
$article_second_em = $em_second->getRepository('AppBundle:Article')->find(2);

For the table prefix I would use a table suscriber

Quite old but still works How to setup table prefix in symfony2 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/sql-table-prefixes.html

Upvotes: 0

Kwido
Kwido

Reputation: 1382

In order to change the table you've got to to update Doctrine's class meta data of that entity.

// getEntityManager() = $this->getDoctrine()->getManager()
$articleMetaData = $this->getEntityManager()->getMetadataFactory()->getMetadataFor(Article::class);
$metaDataBuilder = new ClassMetadataBuilder($articleMetaData);
$metaDataBuilder->setTable('db2_article');
$this->getEntityManager()->getMetadataFactory()
    ->setMetadataFor(Article::class, $metaDataBuilder->getClassMetadata());

$article2MetaData = $this->getEntityManager()->getClassMetadata(Article::class);
$article2MetaData->getTableName(); // is now db2_article
$this->getEntityManager()->find(Article::class, 1); // will query db2_article ID -> 1

To see what the class meta data is up to as in methods, see: Doctrine PHP Mapping

Upvotes: 2

Related Questions