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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?