Doctrine2和Zend框架中的多数据库连接 [英] Multiple database connection in Doctrine2 and Zend framework

查看:247
本文介绍了Doctrine2和Zend框架中的多数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ZF 1.11.3构建的应用程序 - Doctrine2是使用的ORM组件。我需要使用多个数据库。在application.ini文件中,我已经设置了数据库连接,如下所示:

I am currently working on an application which is built using ZF 1.11.3 - Doctrine2 is the ORM component used. I need to use multiple databases. Within the application.ini file, I have set the database connections as follows:

resources.doctrine.dbal.connections.default.parameters.dbname   = "db_name_one"
resources.doctrine.dbal.connections.secondary.parameters.dbname   = "db_name_two"

如何根据第二个数据库连接与该连接关联Doctrine2实体类:例如,如果我有第二个连接的实体类为:

How would I associate the Doctrine2 entity classes based on the second database connection with that connection: For example, if I have an entity class from the second connection as:

/*
   * @Entity
   * @Table(name="tableOnSecondDatabase")
   */
  Class EntityFromSecond
  {

Doctrine2 / ZF将如何知道实体类映射到数据库?感谢您的帮助。

How will Doctrine2/ZF know what entity classes are mapped to a database? Thanks for helping.

推荐答案

我不会瘦,你可以绑定一个实体到特定的连接,这是没有意义的教义2架构。

I don't thinl you can bind an entity to specific connection, it wouldn't make sense in the Doctrine 2 architecture.

另一方面,您可以使用两个实体管理器,每个实体管理器具有不同的连接选项。您必须决定您的业务逻辑,哪个实体被哪个连接管理器处理。

What you can do, on the other hand is to have two EntityManagers, each with different connection options. You must decide in your business logic, which entity is treated by which connection manager.

编辑 Doctrine 2不支持跨数据库连接AFAIK具有两个截然不同的连接和连接的感觉。我甚至无法想象,这将如何在PHP PDO层面上起作用。 Vijay提出的一个基于Doctrine 1和第二个,这不是完全跨数据库连接,因为Doctrine 1执行2个查询并合并结果本身,这不是最佳的性能。

edit Doctrine 2 does not support cross database joins in the sense of having two distinct connections and joining across them, AFAIK. I can't even imagine, how that would work on the PHP PDO level. What Vijay suggested for one based around Doctrine 1 and second, that's not exactly cross database join, as Doctrine 1 executes 2 queries and merges the results itself, which is not optimal performance-wise.

另一方面,您可以做的是具有一个连接,可以访问这两个数据库(即,如果它们在同一个DB服务器上)或者模式,如果你在说Postgres,并且定义你的实体,那么这样做:

What you could do, on the other hand, is to have one connection, which can access both databases (that is, if they're on the same DB server), or schemas, if you're on say Postgres, and define your entities as such:

//defining first entity
@Entity
@Table(firstSchema.table_name)
class MyEntity

//defining second entity

@Entity
@Table(secondSchema.table_name)
class SecondEntity

这应该工作,我相信

这篇关于Doctrine2和Zend框架中的多数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆