問題描述
我目前正在開發一個使用 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 框架中的多數據庫連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!