How to setup table prefix in symfony2

后端 未结 6 1048
[愿得一人]
[愿得一人] 2020-11-27 16:41

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 indi

6条回答
  •  长情又很酷
    2020-11-27 17:24

    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

      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

提交回复
热议问题