2015-10-24 13:28:02 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-30 08:58:39 +00:00
|
|
|
namespace Wallabag\CoreBundle\Event\Subscriber;
|
2015-10-24 13:28:02 +00:00
|
|
|
|
|
|
|
use Doctrine\Common\EventSubscriber;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
|
2015-10-24 13:28:02 +00:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts a prefix to each table.
|
2015-10-25 14:55:19 +00:00
|
|
|
* This way were used instead of using the built-in strategy from Doctrine, using `naming_strategy`
|
|
|
|
* Because it conflicts with the DefaultQuoteStrategy (that espace table name, like user for Postgres)
|
|
|
|
* see #1498 for more detail.
|
2015-10-24 13:28:02 +00:00
|
|
|
*
|
|
|
|
* Solution from :
|
|
|
|
* - http://stackoverflow.com/a/23860613/569101
|
|
|
|
* - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
|
|
|
|
*/
|
|
|
|
class TablePrefixSubscriber implements EventSubscriber
|
|
|
|
{
|
|
|
|
protected $prefix = '';
|
|
|
|
|
|
|
|
public function __construct($prefix)
|
|
|
|
{
|
|
|
|
$this->prefix = (string) $prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubscribedEvents()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
return ['loadClassMetadata'];
|
2015-10-24 13:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $args)
|
|
|
|
{
|
|
|
|
$classMetadata = $args->getClassMetadata();
|
2015-10-25 14:55:19 +00:00
|
|
|
|
2015-10-24 13:28:02 +00:00
|
|
|
// if we are in an inheritance hierarchy, only apply this once
|
|
|
|
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-25 14:55:19 +00:00
|
|
|
$classMetadata->setTableName($this->prefix.$classMetadata->getTableName());
|
2015-10-24 13:28:02 +00:00
|
|
|
|
|
|
|
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
|
|
|
|
if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
|
|
|
|
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
|
2015-10-25 14:55:19 +00:00
|
|
|
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName;
|
2015-10-24 13:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|