Symfony and Doctrine fixtures with single connection and multiple schemas
<p>I'm using Symfony and Doctrine fixtures to create a test database. Our application has a single database connection with 3 schemas and cross schema relationships.</p>

<p>I have 3 bundles setup each with the entity classes and fixtures for each schema. I've been able to create the cross bundle relationships but I am having issues loading fixtures using php bin/console <em>doctrine:fixtures:load</em>. Ideally I would like to be able to run something similar to the following to have my test database setup and teardown:</p>

<pre><code>php bin/console doctrine:database:create
php bin/console doctrine:schema:create / php bin/console doctrine:schema:update
php bin/console doctrine:fixtures:load
php bin/console doctrine:database:drop
</code></pre>

<p>What should my config look like for connections and entity managers? Do I need to run each of the above for each schema and what should my mapping look like if I need multiple entity managers?</p>

<p><strong>Database Structure</strong></p>

<pre><code>- Database
   - Schema1
   - Schema2
   - Schema3
</code></pre>

<p><strong>Bundles</strong></p>

<pre><code>- Acme
   - Bundle
      - Schema1Bundle
         - Entity
         - DataFixtures
             - ORM
      - Schema2Bundle
         - Entity
         - DataFixtures
             - ORM
      - Schema3Bundle
         - Entity
         - DataFixtures
             - ORM
</code></pre>

<p><strong>Composer.json</strong></p>

<pre><code>"require": {
    "php": "&gt;=5.5.9",
    "symfony/symfony": "3.0.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-cache-bundle": "^1.2",
    "symfony/swiftmailer-bundle": "^2.3",
    "symfony/monolog-bundle": "^2.8",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "symfony/console": "^3.0",
    "league/flysystem": "^1.0"
},
"require-dev": {
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^2.7",
    "doctrine/doctrine-fixtures-bundle": "^2.3"
},
</code></pre>

<p><strong>config.yml</strong></p>

<pre><code>orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: schema1

entity_managers:
    schema1:
        auto_mapping: false
        connection: schema1
        mappings:
            AcmeBundleSchema1Bundle: ~
            AcmeBundleSchema2Bundle: ~
            AcmeBundleSchema3Bundle: ~
</code></pre>

<p><strong>Example xml mapping:</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"&gt;
  &lt;entity name="Acme\Bundle\Schema1Bundle\Entity\Currency" table="schema1.currency"&gt;
  &lt;id name="currencyCode" type="string" column="currency_code" length="3"&gt;
      &lt;generator strategy="IDENTITY"/&gt;
  &lt;/id&gt;
  &lt;field name="nsInternalId" type="integer" column="ns_internal_id" nullable="true"&gt;
      &lt;options&gt;
          &lt;option name="unsigned"&gt;1&lt;/option&gt;
      &lt;/options&gt;
   &lt;/field&gt;
   &lt;/entity&gt;
&lt;/doctrine-mapping&gt;
</code></pre>

<p>I've tried using multiple entity managers but when loading fixtures, it attempts to truncate tables in incorrect schemas.</p>

<p><strong>Update:</strong>
I receive the following error when I run doctrine:fixtures:load --em=schema1</p>

<pre><code>An exception occurred while executing 'DELETE FROM boosted_user':
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'schema1.user' doesn't exist
</code></pre>

<p>The user table is in schema2 but for some reason when fixtures:load attempts to remove existing records, it tries to do so for all tables.</p>

<p>I've found that the ORMPurger.php does not use the schema when deleting records on line 141:</p>

<pre><code>$this-&gt;em-&gt;getConnection()-&gt;executeUpdate("DELETE FROM " . $tbl);
</code></pre>