【发布时间】:2018-12-18 13:58:09
【问题描述】:
我有一个名为“位置”的实体,其中包含以下字段:
- id(自动递增)
- 车辆(“车辆”表的外键)
- 纬度
- lng
- 速度
我需要使用学说查询生成器获取按车辆分组的最后位置(带有最后一个 id)。
这是我的功能:
// Find locations
public function findLocations($idFleet, $select, $from, $to)
{
$qb = $this->createQueryBuilder('l')
->innerJoin('l.vehicle', 'v')
->where('v.fleet = :fleet')
->setParameter('fleet', $idFleet)
->andWhere('v.gps = :gps')
->setParameter('gps', true);
// Last locations
if ( $select == "last"){
$qb->groupBy('l.vehicle')
->orderBy('l.id', 'ASC');
}
// else Interval locations
else if ( $select == "interval"){
if( $from != ""){
$from = (new \DateTime())->setTimestamp($from);
$qb->andWhere('l.time >= :from')
->setParameter('from', $from);
}
if( $to != ""){
$to = (new \DateTime())->setTimestamp($to);
$qb->andWhere('l.time <= :to')
->setParameter('to', $to);
}
}
$locations = $qb->getQuery()->getResult();
return $locations;
}
感谢您的帮助。
【问题讨论】:
标签: php sql symfony doctrine greatest-n-per-group