Question

I've currently two separate bundles, one to authenticate incoming connections using oauth2, an another bundle to manage the api/rest calls.

I would like that the different tables associated to each bundle could have their own prefix according to the bundle. For instance having oauth_ for the first bundle and api_ for the other one.

Both services config files are correct (I guess), each of one specifies a different prefix:

<parameter key="MYBUNDLE.db.table_prefix" type="string" >oauth_</parameter> 

and

<parameter key="MYBUNDLE.db.table_prefix" type="string" >api_</parameter> 

Each bundle has also its service entry:

<service id="MYBUNDLE.tblprefix_subscriber" class="MYBUNDLE\Subscriber\TablePrefixSubscriber">
    <argument>%MYBUNDLE.db.table_prefix%</argument>
    <tag name="doctrine.event_subscriber"/>
</service>

and

<service id="MYBUNDLE.tblprefix_subscriber" class="MYBUNDLE\Subscriber\TablePrefixSubscriber">
    <argument>%MYBUNDLE.db.table_prefix%</argument>
    <tag name="doctrine.event_subscriber"/>
</service>

But if I execute the console command to create/update doctrine schema, it just uses api_ prefix, but no oauth, for all the tables.

The subscriber looks like this:

<?php
namespace MYBUNDLE\Subscriber;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;

class TablePrefixSubscriber 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();

        // Do not re-apply the prefix in an inheritance hierarchy.
        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
            return;
        }

        if (FALSE !== strpos($classMetadata->namespace, 'ApiBundle') || FALSE !== strpos($classMetadata->namespace, 'AuthBundle')) {
            $classMetadata->setPrimaryTable(array('name' => $this->prefix . $classMetadata->getTableName()));

            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
                if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY
                  && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
                    $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
                }
            }
        }
    }
}

So, is it possible to have different prefixes? I thought it was possible but I am able to do it...

Thank you in advance...

Was it helpful?

Solution

The problem seems to be that both prefixes share the same parameter key, with the second occurence overwriting the first. I guess you must use two keys, like MYBUNDLE.db.table_prefix_oauth and MYBUNDLE.db.table_prefix_api.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top