doctrine orm multiple databases User entity class not found - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, July 30, 2023

doctrine orm multiple databases User entity class not found

trying to setup postgres alongside mariadb in my symfony 6 project to migrate my database.

following the guide here

and my entity is called User

SO I have setup two databases in doctrine.yaml

doctrine:
    dbal:
        default_connection: mariadb
        connections:
            mariadb:
                server_version:    '%env(DATABASE_VERSION)%'
                dbname:            '%env(DATABASE_NAME)%'
                host:              '%env(DATABASE_HOST)%'
                port:              '%env(DATABASE_PORT)%'
                user:              '%env(DATABASE_USER)%'
                password:          '%env(DATABASE_PASSWORD)%'
                driver:            '%env(DATABASE_DRIVER)%'
                charset:            UTF8
                options:
                    !php/const PDO::MYSQL_ATTR_SSL_KEY: '%DATABASE_PUB_KEY%'
                    !php/const PDO::MYSQL_ATTR_SSL_CERT: '%DATABASE_PRIV_KEY%'
                    !php/const PDO::MYSQL_ATTR_SSL_CA: '%DATABASE_CA_CERT%'
                    !php/const PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT: false
            postgres:
                server_version:    '15'
                dbname:            'mr_dev'
                host:              'postgres'
                port:              '5432'
                user:              '%env(DATABASE_USER)%'
                password:          '%env(DATABASE_PASSWORD)%'
                driver:            'pdo_pgsql'
                charset:            UTF8
    orm:
        default_entity_manager: mariadb
        entity_managers:
            mariadb:
                connection: mariadb
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: true
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
                dql:
                    numeric_functions:
                        rand: DoctrineExtensions\Query\Mysql\Rand
            postgres:
                connection: postgres
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App

and doctrine_migrations.yaml

doctrine_migrations:
    em: 'mariadb'
    migrations_paths:
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
    enable_profiler: '%kernel.debug%'

Then I access like this:

use Doctrine\Persistence\ManagerRegistry;

ManagerRegistry $doctrine

$users = $doctrine->getRepository(User::class, 'mariadb')->findAll();
        dump($users);

error

Class 'App\Controller\home\User' does not exist

it is looking at the wrong folder; my entity is in App\src\Entity\User.php



source https://stackoverflow.com/questions/76793052/doctrine-orm-multiple-databases-user-entity-class-not-found

No comments:

Post a Comment