我试图模仿这个SQL查询:
$sql = "SELECT *, log.id as logid, date_format(shipdate,'%m/%d/%Y') as shipdate
FROM log LEFT JOIN sub ON sub.id = sub_id
LEFT JOIN main ON id = main_id WHERE
shipdate >= '$datestart' AND shipdate <= '$dateend' ";
当我把SELECT *放在Doctrine的createQuery函数中时,一个错误说它没有识别出那个语句所以我试图通过选择单个实体来获取所有实体:
$query = $this->getEntityManager()
->createQuery('
SELECT
m.dano, m.partno, m.batchno,
s.rackno, s.heatcode, s.diecode,
l.shipdate, l.qtyshipped, l.blno, l.id as logid
FROM
Bundle:Sub s
JOIN
s.main m
JOIN
s.log l
WHERE
l.shipdate >= :fromdate and l.shipdate <= :todate
')->setParameter('fromdate', $fromdate)
->setParameter('todate', $todate);
但是,当我这样做时,它不会返回原始查询所做的所有数据。它会跳过一些行。还有另一种方法可以选择所有实体吗?
答案 0 :(得分:0)
您可以使用DQL模仿以下SQL。我相信这是最好的方法,而不是使用创建查询。在DQL中选择all是'select(别名)'。见我的
SELECT *, log.id as logid, date_format(shipdate,'%m/%d/%Y') as shipdate
FROM log LEFT JOIN sub ON sub.id = sub_id
LEFT JOIN main ON id = main_id WHERE
shipdate >= '$datestart' AND shipdate <= '$dateend'
DQL
$dql = $qb2->select('m.dano, m.partno, m.batchno,
s.rackno, s.heatcode, s.diecode,
l.shipdate, l.qtyshipped, l.blno, l.id')
->from('YourBundle:Log','l')
->leftJoin('l.sub', 's', Expr\Join::ON, 's.id = l.sub_id')
->leftJoin('l.main', 'm', Expr\Join::ON, 'm.id = l.main_id')
->where('l.shipdate >= :shipdateStart')
->setParameter('shipdateStart', $datestart)
->where('l.shipdate <= :shipdateEnd')
->setParameter('shipdateEnd', $dateend)
顺便说一下,与创建查询函数中的查询相比,给定的sql示例中的选择表和连接表略有不同。我模仿了上面列出的原始SQL中的内容。希望这有助于您了解将SQL转换为DQL。
此致