开发者

Column aggregation between multiple databases using Doctrine

Let's say I have 2 tables in different databases that represent one model

(one-to-one column aggregation).

schema:

Table1:
  connection: conn1
  columns:
    property1: string
Table2:
  connection: conn2
  columns:
    tabl开发者_运维知识库e1_id: integer
    property2: string
  relations:
    Table2:
      local: table1_id
      foreign: id
      type: one
      foreignType: one

So I can retrieve doctrine collection from one table:

$objects = Doctrine::getTable('Table1')->findAll()

And then retrive properties of each object from another table:

foreach ($objects as $object) 
{
  $object->getProperty2();
}

But, this will result in too many sql requests from Table2 (one request for each object). What I'm trying to achieve is one sql request from each table.

If both tables where in one database, a simple join would do the trick. Any suggestions?


If anyone intrested, here's a solution: basically you need to execute 2 separate queries resulting in 2 separate result sets, and then join them, using Doctrine_Collection::populateRelated() method.

$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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜