symfony3 - Ask the master: How to ignore the table prefix when symfony generates entities from the database
PHPz
PHPz 2017-05-16 16:43:49
0
1
730

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”

PHPz
PHPz

学习是最好的投资!

reply all(1)
習慣沉默

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

<?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;
            }
        }
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template