stoneynn
stoneynn

Reputation: 43

doctrine2 multiple db connect in zf3?

I used doctrine2 in zf3, while connect multiple db caused error. Then following is my config in global.php

return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '127.0.0.1',
                    'user'     => 'root',
                    'password' => '123456',
                    'dbname'   => 'zf3.com',
                    'charset' => 'utf8',
                ]
            ],
            'orm_passport' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '127.0.0.1',
                    'user'     => 'root',
                    'password' => '123456',
                    'dbname'   => 'zf3.com.passport',
                    'charset' => 'utf8',
                ]
            ],
        ],
        'entitymanager' => [
            'orm_passport' => [
                'connection' => 'orm_passport',
            ]
        ],
    ],
];

And driver config in module.config.php as following:

'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [__DIR__ . '/../src/Entity']
            ],
            'orm_passport' => [
                'drivers' => [
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ]
            ]
        ]
    ],

In my IndexController.php

 public function indexAction()
    {
        // Get recent users
        $users = $this->entityManager->getRepository(Users::class)
            ->findBy(['status'=>Users::ACTIVE_STATUS_NO],['timeCreated'=>'DESC']);

        //\Doctrine\Common\Util\Debug::dump($users);

        return new ViewModel([
            'users' => $users
        ]);
    }

The error message : The class 'Passport\Entity\Users' was not found in the chain configured namespaces Application\Entity

Upvotes: 3

Views: 988

Answers (1)

stoneynn
stoneynn

Reputation: 43

the following is my global.php

return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '127.0.0.1',
                    'user'     => 'root',
                    'password' => '123456',
                    'dbname'   => 'zf3.com',
                    'charset' => 'utf8',
                ]
            ],
            'orm_passport' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '127.0.0.1',
                    'user'     => 'root',
                    'password' => '123456',
                    'dbname'   => 'zf3.com.passport',
                    'charset' => 'utf8',
                ]
            ],
        ],
        'entitymanager' => [
            'orm_passport' => [
                'connection' => 'orm_passport',
                'configuration' => 'orm_passport',
            ]
        ],
        'migrations_configuration' => [
            'orm_passport' => [
                'directory' => 'data/DoctrineORMModule/Migrations',
                'name' => 'Doctrine Database Migrations',
                'namespace' => 'DoctrineORMModule\\Migrations',
                'table' => 'migrations',
                'column' => 'version',
            ],
        ],
        'configuration' => [
            'orm_passport' => [
                'metadata_cache' => 'array',
                'query_cache' => 'array',
                'result_cache' => 'array',
                'hydration_cache' => 'array',
                'driver' => 'orm_passport',
                'generate_proxies' => true,
                'proxy_dir' => 'data/DoctrineORMModule/Proxy',
                'proxy_namespace' => 'DoctrineORMModule\\Proxy',
            ]
        ],
        'authentication' => [
            'odm_passport' => [],
            'orm_passport' => [
                'objectManager' => 'doctrine.entitymanager.orm_passport',
            ],
        ],
        'authenticationadapter' => [
            'odm_passport' => true,
            'orm_passport' => true,
        ],
        'authenticationstorage' => [
            'odm_passport' => true,
            'orm_passport' => true,
        ],
        'authenticationservice' => [
            'odm_passport' => true,
            'orm_passport' => true,
        ],
    ],
];

it works!

Upvotes: 1

Related Questions