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

Multiple table prefixes - Version constructor argument #3123

Closed
matesko opened this issue Mar 16, 2022 · 1 comment
Closed

Multiple table prefixes - Version constructor argument #3123

matesko opened this issue Mar 16, 2022 · 1 comment

Comments

@matesko
Copy link
Contributor

matesko commented Mar 16, 2022

When using multiple entity managers and database connections Bolt\Doctrine\Versions constructor throws error

Argument #2 ($tablePrefix) must be of type string, array given.

Bolt Version 5.1.4

To reproduce:

.env

DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/bolt.sqlite
DATABASE_URL2=sqlite:///%kernel.project_dir%/var/data/bolt2.sqlite

doctrine.yaml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                charset: utf8mb4
                url: '%env(resolve:DATABASE_URL)%'
            d2:
                charset: utf8mb4
                url: '%env(resolve:DATABASE_URL2)%'
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'

    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: true
                mappings:
                    Bolt:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/vendor/bolt/core/src/Entity'
                        prefix: 'Bolt\Entity'
                        alias: Bolt
                dql:
                    string_functions:
                        JSON_EXTRACT: Bolt\Doctrine\Functions\JsonExtract
                        JSON_GET_TEXT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\JsonGetText
                        JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch
                        CAST: Bolt\Doctrine\Query\Cast
                    numeric_functions:
                        RAND: Bolt\Doctrine\Functions\Rand
            d2:
                connection: d2
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App

services.yaml

    app.table_prefix:
        default: bolt_
        d2: foo_
...
       bind:              
            $tablePrefix: '%app.table_prefix%'

@matesko
Copy link
Contributor Author

matesko commented Mar 16, 2022

How I fixed it:

Version.php

    public function __construct(Connection $connection, $tablePrefix = 'bolt')
    {
        $this->connection = $connection;
        $tablePrefix = is_array($tablePrefix) ? $tablePrefix['default'] : $tablePrefix;
        $this->tablePrefix = Str::ensureEndsWith($tablePrefix, '_');
    }

TablePrefixTrait.php

    protected function setTablePrefix(ObjectManager $manager, string $prefix)
    {
        $key = spl_object_hash($manager);
        $this->tablePrefixes[$key] = empty($prefix) ? $prefix : Str::ensureEndsWith($prefix, '_');

        return $this;
    }
...
    protected function getTablePrefix(ObjectManager $manager): string
    {
        $key = spl_object_hash($manager);

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

TablePrefix.php

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

@matesko matesko closed this as completed Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant