使用postgres“OVERLAPS”自定义学说扩展

时间:2018-05-24 13:11:26

标签: postgresql symfony doctrine-orm doctrine symfony-3.4

我正在尝试将postgres中的OVERLAPS函数实现为3.4 Symfony项目以进行日期范围匹配。

我做了以下扩展:

class DateOverlaps extends FunctionNode
{
    public $rangeStart;
    public $rangeEnd;
    public $targetStart;
    public $targetEnd;


    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->rangeStart = $parser->InputParameter();
        $parser->match(Lexer::T_COMMA);
        $this->rangeEnd = $parser->InputParameter();
        $parser->match(Lexer::T_COMMA);
        $this->targetStart = $parser->SingleValuedPathExpression();
        $parser->match(Lexer::T_COMMA);
        $this->targetEnd = $parser->SingleValuedPathExpression();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    /**
     * @param SqlWalker $sqlWalker
     *
     * @return string
     */
    public function getSql(SqlWalker $sqlWalker)
    {
        $rangeStart = $this->rangeStart->dispatch($sqlWalker);
        $rangeEnd = $this->rangeEnd->dispatch($sqlWalker);
        $targetStart = $this->targetStart->dispatch($sqlWalker);
        $targetEnd = $this->targetEnd->dispatch($sqlWalker);

        return "($rangeStart, $rangeEnd) OVERLAPS ($targetStart, $targetEnd)";
    }
}

配置:

orm:
    dql:
        string_functions:
            overlaps: AppBundle\Driver\DateOverlaps

并在像这样的简单存储库中使用它:

public function findRangedEvents(\DateTime $start, \DateTime $end)
{
    $qb = $this->createQueryBuilder('event')
        ->where("overlaps(:start, :end, event.occuringDate, event.occuringEndDate)")
        ->setParameter('start', $start)
        ->setParameter('end', $end);

    return $qb->getQuery()->getResult();
}

但是我得到了像

这样的错误
"[Syntax Error] line 0, col -1: Error: Expected =, <, <=, <>, >, >=, !=, got end of string."

我尝试了很多不同的解析器函数,但我真的无法理解我所缺少的东西。

1 个答案:

答案 0 :(得分:0)

您可以通过以下条件解决该问题

$qb->where("overlaps(:start, :end, event.occuringDate, event.occuringEndDate) = true")