DQL在查询中执行联合

时间:2014-04-14 08:38:41

标签: doctrine-orm union dql

我知道这个问题有几位亲戚,但我还是不能做我的!

我有两张桌子(工作室和模特)。我想为我的数据表执行联合。

目前,我有这个:

$cnn = $this->Doctrine->getConnection(); 


    $inscr = $cnn->fetchAll("(SELECT s.thedate AS mydate, s.name AS designation, \'Studio\' AS mytype
             FROM studios s ORDER BY mydate LIMIT 2)
                UNION
            (SELECT m.thedate AS mydate, m.nickname AS designation, \'Hotesse\' AS mytype
             FROM  models m ORDER BY mydate LIMIT 2
            )");


    return $inscr;

但我的数据表中没有任何内容,我无法执行任何var_dump或其他操作。

我在我的RDBMS中测试了这个,我得到了等待的结果。那么,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了执行查询并获得结果的解决方案

  $sql = "(SELECT s.thedate AS mydate, s.name AS designation, 'Studio' AS mytype
 FROM studios s ORDER BY mydate LIMIT 2)
    UNION
(SELECT m.thedate AS mydate, m.nickname AS designation, 'Hotesse' AS mytype
 FROM  models m ORDER BY mydate LIMIT 2
)";


    $stmt = $this->em->getConnection()->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll();

但是,不幸地在datatable中,结果必须是Doctrine \ ORM \ QueryBuilder的一个实例,我的解决方案返回一个数组..

如果有人对我的特殊情况有所了解,我就在这里!