DDC-153: add a database table prefix option #191

Closed
opened 2026-01-22 12:30:07 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 16, 2009).

Jira issue originally created by user itoijala:

A nice option would be the ability to set a prefix for database table names.

Example:

entity table name (@Table) = users

prefix = foo_

=> table name in database = foo . name = foo_users

The option should default to an empty string.

This would make it possible to run multiple instances of the same application in a single database and remove naming conflicts between apps (many apps will want tot use the table users, for example). Each application could be configured to use a custom prefix. Without this feature, changes in the code in many places would be required, making updating the app difficult. This would be especially useful in a shared hosting environment where every user only has one database.

Originally created by @doctrinebot on GitHub (Nov 16, 2009). Jira issue originally created by user itoijala: A nice option would be the ability to set a prefix for database table names. Example: entity table name (@Table) = users prefix = foo_ => table name in database = foo . name = foo_users The option should default to an empty string. This would make it possible to run multiple instances of the same application in a single database and remove naming conflicts between apps (many apps will want tot use the table users, for example). Each application could be configured to use a custom prefix. Without this feature, changes in the code in many places would be required, making updating the app difficult. This would be especially useful in a shared hosting environment where every user only has one database.
admin added the New Feature label 2026-01-22 12:30:07 +01:00
admin closed this issue 2026-01-22 12:30:07 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 7, 2010):

Comment created by shurakai:

Personally, I believe that today even the cheapest (or even free?) webspace packages ship with more than one database.

Additionally, it is my strong believe that it's a good thing to separate different applications into different databases.

Therefore, I strongly recommend to not fix this issue. It would simply bloat the code but there's no further use in it IMHO.

@doctrinebot commented on GitHub (May 7, 2010): Comment created by shurakai: Personally, I believe that today even the cheapest (or even free?) webspace packages ship with more than one database. Additionally, it is my strong believe that it's a good thing to separate different applications into different databases. Therefore, I strongly recommend to not fix this issue. It would simply bloat the code but there's no further use in it IMHO.
Author
Owner

@doctrinebot commented on GitHub (May 12, 2010):

Comment created by merk:

I am currently working on an application that will integrate multiple different vendor products into one system, all with their own ability to have prefixed tables.

While in my application the prefix is fixed, I do see some benefit in having this feature. But as romanb says in IRC, I think it should be implemented as a listener, maybe a cookbook doc entry?:

09:13 http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#load-classmetadata-event
09:13 this event in particular
09:14 just set the table name to a new one, with the prefix
09:14 in the listener
09:15 sth like $class = $eventArgs->getClassMetadata(); $class->setTableName('prefix_' . $class->getTableName());

@doctrinebot commented on GitHub (May 12, 2010): Comment created by merk: I am currently working on an application that will integrate multiple different vendor products into one system, all with their own ability to have prefixed tables. While in my application the prefix is fixed, I do see some benefit in having this feature. But as romanb says in IRC, I think it should be implemented as a listener, maybe a cookbook doc entry?: 09:13 <romanb> http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#load-classmetadata-event 09:13 <romanb> this event in particular 09:14 <romanb> just set the table name to a new one, with the prefix 09:14 <romanb> in the listener 09:15 <romanb> sth like $class = $eventArgs->getClassMetadata(); $class->setTableName('prefix_' . $class->getTableName());
Author
Owner

@doctrinebot commented on GitHub (May 13, 2010):

Comment created by @beberlei:

Cookbook sounds good.

@doctrinebot commented on GitHub (May 13, 2010): Comment created by @beberlei: Cookbook sounds good.
Author
Owner

@doctrinebot commented on GitHub (May 13, 2010):

Comment created by merk:

I've come up with something simple, and if noone else wants to tackle it I'll give it a go at writing a cookbook entry:

## DoctrineExtensions/TablePrefix.php

<?php

namespace DoctrineExtensions;
use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;

class TablePrefix
{
    protected $_prefix = '';

    public function **construct($prefix)
    {
        $this->_prefix = (string) $prefix;
    }

    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        $classMetadata = $eventArgs->getClassMetadata();
        $classMetadata->setTableName($this->_prefix . $classMetadata->getTableName());
    }
}
## Bootstrap

<?php

// ..After $em has been created and returned..
$evm = $em->getEventManager();

// Table Prefix
$tablePrefix = new \DoctrineExtensions\TablePrefix('prefix_');
$evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix);
@doctrinebot commented on GitHub (May 13, 2010): Comment created by merk: I've come up with something simple, and if noone else wants to tackle it I'll give it a go at writing a cookbook entry: ``` ## DoctrineExtensions/TablePrefix.php <?php namespace DoctrineExtensions; use \Doctrine\ORM\Event\LoadClassMetadataEventArgs; class TablePrefix { protected $_prefix = ''; public function **construct($prefix) { $this->_prefix = (string) $prefix; } public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { $classMetadata = $eventArgs->getClassMetadata(); $classMetadata->setTableName($this->_prefix . $classMetadata->getTableName()); } } ``` ``` ## Bootstrap <?php // ..After $em has been created and returned.. $evm = $em->getEventManager(); // Table Prefix $tablePrefix = new \DoctrineExtensions\TablePrefix('prefix_'); $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix); ```
Author
Owner

@doctrinebot commented on GitHub (May 14, 2010):

Comment created by @beberlei:

You should make sure the event is passed to the EM factory, otherwise an entity might already be loaded, without the event..

@doctrinebot commented on GitHub (May 14, 2010): Comment created by @beberlei: You should make sure the event is passed to the EM factory, otherwise an entity might already be loaded, without the event..
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by @beberlei:

Documentation issue of this was fixed by merk, there is now a cookbook entry describing how to implement table prefixes.

@doctrinebot commented on GitHub (May 16, 2010): Comment created by @beberlei: Documentation issue of this was fixed by merk, there is now a cookbook entry describing how to implement table prefixes.
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (May 16, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#191