Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:47:24+00:00 2026-05-27T06:47:24+00:00

Like in question topic, how can I setup default table prefix in symfony2? The

  • 0

Like in question topic, how can I setup default table prefix in symfony2?

The best if it can be set by default for all entities, but with option to override for individual ones.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:47:25+00:00Added an answer on May 27, 2026 at 6:47 am

    Having just figured this out myself, I’d like to shed some light on exactly how to accomplish this.

    Symfony 2 & Doctrine 2.1

    Note: I use YML for config, so that’s what I’ll be showing.

    Instructions

    1. Open up your bundle’s Resources/config/services.yml

    2. Define a table prefix parameter:

      Be sure to change mybundle and myprefix_

      parameters:
          mybundle.db.table_prefix: myprefix_
      
    3. Add a new service:

      services:
          mybundle.tblprefix_subscriber:
              class: MyBundle\Subscriber\TablePrefixSubscriber
              arguments: [%mybundle.db.table_prefix%]
              tags:
                  - { name: doctrine.event_subscriber }
      
    4. Create MyBundle\Subscriber\TablePrefixSubscriber.php

      <?php
      namespace MyBundle\Subscriber;
      
      use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
      
      class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
      {
          protected $prefix = '';
      
          public function __construct($prefix)
          {
              $this->prefix = (string) $prefix;
          }
      
          public function getSubscribedEvents()
          {
              return array('loadClassMetadata');
          }
      
          public function loadClassMetadata(LoadClassMetadataEventArgs $args)
          {
              $classMetadata = $args->getClassMetadata();
              if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
                  // if we are in an inheritance hierarchy, only apply this once
                  return;
              }
      
              $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
      
              foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
                  if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY 
                          && array_key_exists('name', $classMetadata->associationMappings[$fieldName]['joinTable']) ) {     // Check if "joinTable" exists, it can be null if this field is the reverse side of a ManyToMany relationship
                      $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
                      $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
                  }
              }
          }       
      }
      
    5. Optional step for postgres users: do something similary for sequences

    6. Enjoy
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's quite possible a question like this has been asked before, but I can't
I know a question like this was already asked, but the situation is a
This is more like a question of principle. I made a table with 100%
You may think this question is like this question asked on StackOverflow earlier. But
This is a rather general question .. What hardware setup is best for large
This is my second question about this topic, the original question can be found
This may seem like a dumb question, but I'm in a head-> wall situation
First of all this is not a question about how can I use http
Pretty simple question, but I can't seem to figure out how to do this,
I've read every question and answer on this topic I can find on this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.