Baseret på svaret herfra:Sådan opsætter du tabel præfiks i symfony2
-
Åbn din bundts Resources/config/services.yml og tilføj:
parameters: mybundle.db.sequence_prefix: myprefix_
-
Tilføj en ny tjeneste:
services: mybundle.sqprefix_subscriber: class: MyBundle\Subscriber\SequencePrefixSubscriber arguments: [%mybundle.db.sequence_prefix%] tags: - { name: doctrine.event_subscriber }
-
Opret MyBundle\Subscriber\SequencePrefixSubscriber.php
<?php namespace MyBundle\Subscriber; use Doctrine\ORM\Event\LoadClassMetadataEventArgs; class SequencePrefixSubscriber 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()) { return; } if ($classMetadata->isIdGeneratorSequence()) { $newDefinition = $classMetadata->sequenceGeneratorDefinition; $newDefinition['sequenceName'] = $this->prefix . $newDefinition['sequenceName']; $classMetadata->setSequenceGeneratorDefinition($newDefinition); $em = $args->getEntityManager(); if (isset($classMetadata->idGenerator)) { $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator( $em->getConfiguration()->getQuoteStrategy()->getSequenceName( $newDefinition, $classMetadata, $em->getConnection()->getDatabasePlatform()), $newDefinition['allocationSize'] ); $classMetadata->setIdGenerator($sequenceGenerator); } } } }
Reference:http://www. .doctrine-project.org/api/orm/2.0/class-Doctrine.ORM.Mapping.ClassMetadataInfo.html
Dokumentationen siger READ-ONLY
for sequenceGeneratorDefinition
ejendom, så jeg synes, det er renere at bruge sætteren end at ændre værdien manuelt (men der er ingen getter, så vi bruger bare den offentlige ejendomsværdi).