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

查看:17
本文介绍了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.

推荐答案

我不认为您可以将实体绑定到特定连接,这在 Doctrine 2 架构中没有意义.

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

另一方面,您可以做的是拥有两个 EntityManager,每个都有不同的连接选项.您必须在业务逻辑中决定哪个实体由哪个连接管理器处理.

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.

编辑 原则 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.

另一方面,你可以做的是建立一个连接,它可以访问两个数据库(也就是说,如果它们在同一个数据库服务器上)或模式,如果你在说 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天全站免登陆