在Doctrine和PostgreSQL中跨不同服务器上的不同数据库连接


Join across different databases on different servers in Doctrine and PostgreSQL

是否有办法做到dblink在Doctrine中的作用?

我需要连接两个服务器上的两个数据库中的两个表,并且不想使用RawSql,而是使用模型。

Thanks in advance

你想要的东西在Doctrine中并不是"技术上"支持的。

如果你想要像使用传统的PostgreSQL那样做:

SELECT table1.field1, table1.field2, table2.field2, 
    FROM table1 INNER JOIN 
        dblink('dbname=db2 port=5432 host=domainname2 
                user=someuser password=somepwd',
                'SELECT field1, field2,
                FROM other_table')
            AS table2(field1 int, field2 char(25))
        ON table1.field3 = table2.field1;

您可以将config.php文件中默认的单个连接替换为两个连接。

<?php
Doctrine_Manager::connection('mysql://root@server1/doctrine_db1', 'db1');
Doctrine_Manager::connection('mysql://root@server2/doctrine_db2', 'db2');

然后你可以这样修改你的模式:

---
Table1:
    tableName: db1.table1
    connection: db1
    columns:
        filed1: integer
        field2: string(255)
        field3: integer
    relations:
        Table2:
        foreignType: one
        onDelete: CASCADE
Table2:
    tableName: db2.table2
    connection: db2
    columns:
        field1: integer
        field2: string(25)

然后像往常一样添加数据后构建数据库。然后,只需进行常规建模,确保使用您想要的字段指定表,就像使用任何常规查询一样。

在Doctrine博客上详细讨论了Doctrine跨数据库连接。你可以查看一下