Berdasarkan jawaban dari sini:Cara menyiapkan tabel awalan di symfony2
-
Buka Resources/config/services.yml bundel Anda dan tambahkan:
parameters: mybundle.db.sequence_prefix: myprefix_
-
Tambahkan layanan baru:
services: mybundle.sqprefix_subscriber: class: MyBundle\Subscriber\SequencePrefixSubscriber arguments: [%mybundle.db.sequence_prefix%] tags: - { name: doctrine.event_subscriber }
-
Buat 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); } } } }
Referensi:http://www .doctrine-project.org/api/orm/2.0/class-Doctrine.ORM.Mapping.ClassMetadataInfo.html
Dokumentasi mengatakan READ-ONLY
untuk sequenceGeneratorDefinition
properti jadi saya pikir lebih bersih menggunakan setter daripada mengubah nilainya secara manual (tetapi tidak ada pengambil jadi kami hanya menggunakan nilai properti publik).