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”
習慣沉默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
<?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;
}
}
}
}