Php 使用条令在多个数据库之间进行列聚合
假设我在不同的数据库中有两个表,它们代表一个模型Php 使用条令在多个数据库之间进行列聚合,php,doctrine,Php,Doctrine,假设我在不同的数据库中有两个表,它们代表一个模型 (一对一列聚合) 模式: Table1: connection: conn1 columns: property1: string Table2: connection: conn2 columns: table1_id: integer property2: string relations: Table2: local: table1_id foreign: id
(一对一列聚合) 模式:
Table1:
connection: conn1
columns:
property1: string
Table2:
connection: conn2
columns:
table1_id: integer
property2: string
relations:
Table2:
local: table1_id
foreign: id
type: one
foreignType: one
因此,我可以从一个表中检索条令集合:
$objects = Doctrine::getTable('Table1')->findAll()
然后从另一个表中检索每个对象的属性:
foreach ($objects as $object)
{
$object->getProperty2();
}
但是,这将导致来自表2的sql请求过多(每个对象一个请求)。
我试图实现的是来自每个表的一个sql请求
如果两个表都在一个数据库中,那么简单的连接就可以了。
有什么建议吗?如果有人感兴趣,这里有一个解决方案:基本上,您需要执行两个单独的查询,生成两个单独的结果集,然后使用Doctrine_Collection::populateRelated()方法将它们连接起来
$res_set1 = Doctrine::getTable('Table1')->findAll()
$res_set2 = Doctrine::getTable('Table2')
->createQuery('t')
->whereIn('t.table1_id', $res_set1->getPrimaryKeys())
->execute();
$res_set1->populateRelated('Table2', $res_set2);