Skip to content

Re-enable multiple db table prefixes? - possible fix for old problem with identifying EntityManagers #2205

@simongroenewolt

Description

@simongroenewolt

Will elaborate later.

When digging in the code for a way to specify different prefixes for different EntityManagers (a question about prefixes was raised on slack...) I found that this used to be possible using https://github.com/bolt/core/blob/master/src/Doctrine/TablePrefix.php and https://github.com/bolt/core/blob/master/src/Doctrine/TablePrefixTrait.php

Apparently after the introduction of symfony/proxy-manager-bridge this functionality stopped working 202bd79

I think I've found a fix :-)

I did a little experiment, and found that when you do this:

    protected function setTablePrefix(ObjectManager $manager, string $prefix)
    {
        $key = spl_object_hash($manager);
        // force initializing the ObjectManager in case it is a proxy
        $manager->getMetadataFactory();
        $key2 = spl_object_hash($manager);
        $this->tablePrefixes[$key] = Str::ensureEndsWith($prefix, '_');

        return $this;
    }

$key2 will have the 'correct' spl_object_hash() result.

Why? Well, the original call to spl_object_hash() was before the lazy proxy was initialized. Performing an action on the proxy will initialize it. Apparently this initialization is on a level where it actually means the spl_object_hash does change, even when the documentation of spl_object_hash() suggests to me that such a thing would not be possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions