search

Home  >  Q&A  >  body text

symfony3 - Ask the master: How to ignore the table prefix when symfony generates entities from the database

Ask the master: How to ignore the table prefix when generating entities from the database?

For example, t_test directly generates Test when generating entity.

Is there any way to solve this problem? Thanks!

“php bin/console doctrine:mapping:import --force AdminBundle annotation”

PHPzPHPz2853 days ago778

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 16:45:49

    I haven’t found the table prefix configuration in the configuration items. By default t_test 只能生成 entity Test.

    You can refer to Configuring Prefix Processing or this SQL-Table Prefixes, which adds table prefix service processing.

    Simply put:
    Modify app/config/services.yml

    Then append the file src/AppBundle/Subscriber/TablePrefixSubscriber.php ,其中 AppBundle and change it to your own path.
    The content of the file is

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    <code><?php

    namespace AppBundle\Subscriber;

     

    use Doctrine\ORM\Event\LoadClassMetadataEventArgs;

    use Doctrine\Common\EventSubscriber;

     

    class TablePrefixSubscriber implements EventSubscriber

    {

        protected $prefix = '';

     

        public function __construct($prefix)

        {

            $this->prefix = (string) $prefix;

        }

     

        public function getSubscribedEvents()

        {

            return array('loadClassMetadata');

        }

     

        public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)

        {

            $classMetadata = $eventArgs->getClassMetadata();

     

            if (!$classMetadata->isInheritanceTypeSingleTable() || $classMetadata->getName() === $classMetadata->rootEntityName) {

                $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());

            }

     

            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {

                if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide']) {

                    $mappedTableName = $mapping['joinTable']['name'];

                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;

                }

            }

        }

    }</code>

    reply
    0
  • Cancelreply