Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Having just figured this out myself, I'd like to shed some light on exactly how to accomplish this. </p> <p><strong>Symfony 2 &amp; Doctrine 2.1</strong> <br> <sub>Note: I use YML for config, so that's what I'll be showing.</sub><br><br></p> <h1>Instructions</h1> <ol> <li><p>Open up your bundle's <em>Resources/config/services.yml</em></p></li> <li><p>Define a table prefix parameter: <br> <sub>Be sure to change <em>mybundle</em> and <em>myprefix_</em></sub></p> <pre><code>parameters: mybundle.db.table_prefix: myprefix_ </code></pre></li> <li><p>Add a new service:</p> <pre><code>services: mybundle.tblprefix_subscriber: class: MyBundle\Subscriber\TablePrefixSubscriber arguments: [%mybundle.db.table_prefix%] tags: - { name: doctrine.event_subscriber } </code></pre></li> <li><p>Create <em>MyBundle\Subscriber\TablePrefixSubscriber.php</em></p> <pre><code>&lt;?php namespace MyBundle\Subscriber; use Doctrine\ORM\Event\LoadClassMetadataEventArgs; class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber { protected $prefix = ''; public function __construct($prefix) { $this-&gt;prefix = (string) $prefix; } public function getSubscribedEvents() { return array('loadClassMetadata'); } public function loadClassMetadata(LoadClassMetadataEventArgs $args) { $classMetadata = $args-&gt;getClassMetadata(); if ($classMetadata-&gt;isInheritanceTypeSingleTable() &amp;&amp; !$classMetadata-&gt;isRootEntity()) { // if we are in an inheritance hierarchy, only apply this once return; } $classMetadata-&gt;setTableName($this-&gt;prefix . $classMetadata-&gt;getTableName()); foreach ($classMetadata-&gt;getAssociationMappings() as $fieldName =&gt; $mapping) { if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY &amp;&amp; array_key_exists('name', $classMetadata-&gt;associationMappings[$fieldName]['joinTable']) ) { // Check if "joinTable" exists, it can be null if this field is the reverse side of a ManyToMany relationship $mappedTableName = $classMetadata-&gt;associationMappings[$fieldName]['joinTable']['name']; $classMetadata-&gt;associationMappings[$fieldName]['joinTable']['name'] = $this-&gt;prefix . $mappedTableName; } } } } </code></pre></li> <li><p>Optional step for postgres users: <a href="https://stackoverflow.com/questions/19380177/doctrine-how-to-prefix-sequence-names">do something similary for sequences</a></p></li> <li>Enjoy</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload