Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable multiple db table prefixes #2206

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Doctrine/TablePrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct($tablePrefix, ManagerRegistry $managerRegistry)

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
if ($tablePrefix = $this->getTablePrefix()) {
if ($tablePrefix = $this->getTablePrefix($eventArgs->getEntityManager())) {
$classMetadata = $eventArgs->getClassMetadata();

if (! $classMetadata->isInheritanceTypeSingleTable()
Expand Down
20 changes: 12 additions & 8 deletions src/Doctrine/TablePrefixTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ trait TablePrefixTrait

protected function setTablePrefix(ObjectManager $manager, string $prefix)
{
// Force initializing the ObjectManager by calling a method in case it is a proxy for
// a lazily initialized service using symfony/proxy-manager-bridge.
// Doing this before calling spl_object_hash() makes sure we are getting the 'correct' hash
$manager->getMetadataFactory();
$key = spl_object_hash($manager);
$this->tablePrefixes[$key] = Str::ensureEndsWith($prefix, '_');

Expand All @@ -46,14 +50,14 @@ protected function setTablePrefixes($tablePrefixes, ManagerRegistry $managerRegi
return $this;
}

/**
* Since we introduced `symfony/proxy-manager-bridge`, the keys in the tableprefix
* no longer match what the manager tells us it should be. For example, the
* given key was `0000000005ee10ad0000000043b453e3`, but in our reference
* table we had `0000000005ee10e90000000043b453e3`. We just return the first one, now
*/
protected function getTablePrefix(): string
protected function getTablePrefix(ObjectManager $manager)
{
return current($this->tablePrefixes) ?? '';
// Force initializing the ObjectManager by calling a method in case it is a proxy for
// a lazily initialized service using symfony/proxy-manager-bridge.
// Doing this before calling spl_object_hash() makes sure we are getting the 'correct' hash
$manager->getMetadataFactory();
$key = spl_object_hash($manager);

return $this->tablePrefixes[$key] ?? '';
}
}