在zf3中doctrine2多个db连接?

时间:2017-05-04 07:19:21

标签: database doctrine-orm zend-framework3

我在zf3中使用了doctrine2,而连接多个db导致了错误。 接下来是我在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',
            ]
        ],
    ],
];

并在module.config.php中配置驱动程序,如下所示:

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

在我的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
        ]);
    }

错误讯息: 在链配置的命名空间Application \ Entity

中找不到“Passport \ Entity \ Users”类

1 个答案:

答案 0 :(得分:1)

以下是我的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,
        ],
    ],
];

它有效!