Doctrine 2 - 使用UNION进行NativQuery

时间:2012-03-08 15:33:16

标签: doctrine-orm entity union

我希望为实体保留UNION个结果。我的问题是,UNION我在结果中没有别名?

public function getDialogHistoryQuery($addr, $page = 1, $count = 10) {
    $count = (int) abs($count);
    $page = max((int) $page, 1);
    $limitStart = (int)($page - 1) * $count;
    $sql = "(SELECT 
              'in' AS direction, ib.id, ib.from_addr AS addr, ib.receive_time AS time, ib.message
            FROM 
              smsd_inbound AS ib 
            WHERE
              ib.from_addr = :addr)
            UNION ALL
            (SELECT 
              'out' AS direction, ob.id, ob.to_addr, ob.log_time, ob.message
            FROM 
              smsd_outbound AS ob 
            WHERE
              ob.to_addr = :addr)
            ORDER BY time
            LIMIT :limitStart, :limitCount";
    $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getRegistry()->getEntityManager());
    $rsm->addRootEntityFromClassMetadata('SkyDiablo\Util\SMSBundle\Entity\SMSDialog', $alias = 'ib'); //dose not work?
    $query = $this->getRegistry()->getEntityManager()->createNativeQuery($sql, $rsm);
    $query->setParameter('addr', (string)$addr);
    $query->setParameter('limitStart', $limitStart);
    $query->setParameter('limitCount', $count);
    return $query;
}

SQL查询工作正常,但在这种情况下它没有结果。我调试了它,doctrine 2得到了正确的结果但是不能给实体补充水分? 我的“别名”只是尝试获取任何内容,但UNION没有别名?

任何提示?

0 个答案:

没有答案