drox90250557 2017-03-15 07:07
浏览 14
已采纳

使用LEAST和GREATEST函数将SQL转换为Doctrine

I need your help on how am I going to convert the following Mysql to Doctrine.

select * from calendar_data as c where LEAST(c.end, end) - GREATEST(c.start, start) > 0;

I tried this one:

$qb = $em->createQueryBuilder();
$query = $qb->select('items')
                ->from('\Admin\Entity\CalendarData','items')
                ->where('LEAST(items.end, :end) - GREATEST(items.start, :start) > 0')
                ->setParameter('start',$start)
                ->setParameter('end', $end);

(Given that $start and $end is already provided)

and I have the following error:

{
 "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
 "title": "Internal Server Error",
 "status": 500,
 "detail": "[Syntax Error] line 0, col 57: Error: Expected known function,   got 'LEAST'"
}

How should I do that?

This is the generated sql to my question:

Mysql query to determine if the given datetime is included in the datetime interval

  • 写回答

2条回答 默认 最新

  • douniuta4783 2017-03-15 07:30
    关注

    There is no LEAST and GREATES functions in Doctrine. you can rewrite your query to achieve the same result or use ResultSetMapping, like

    use Doctrine\ORM\Query\ResultSetMapping;
    
    $start = '';
    $end = '';
    $rsm = new ResultSetMapping();
    $rsm->addEntityResult('CalendarData', 'c');
    $rsm->addFieldResult('c', 'id', 'id');
    $rsm->addFieldResult('c', 'start', 'start');
    $rsm->addFieldResult('c', 'end', 'end');
    
    $query = $this->_em->createNativeQuery('select * from calendar_data where LEAST(c.end, ?) - GREATEST(c.start, ?) > 0;', $rsm);
    $query->setParameter(1, $end);
    $query->setParameter(2, $start);
    
    $calendarData = $query->getResult();
    

    Read about native sql in doctrine here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器