比较mysql/symfony2中的日期

Man*_*anu 1 php mysql symfony

我已经找到了这个,并且有一些相关的问题,但没有人给我我需要的答案.我有一个带日期字段的实体,我需要选择那些超过7天的人:

$query = $repository->createQueryBuilder('rf')
                    ->where('rf.sendDate >='.new \DateTime('-7 days'))
                    ->getQuery();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Catchable Fatal Error: Object of class DateTime could not be converted to string
Run Code Online (Sandbox Code Playgroud)

我想知道为什么它假定这rf.sendDate是一个字符串,何时被定义为实体中的DateTime对象?我怎么能比较这个?

任何解释真的很感激.

wor*_*nga 6

您应该使用以下参数:

    $query = $repository->createQueryBuilder('rf')
            ->where('rf.sendDate >= :ts')
                ->setParameter('ts', new \DateTime('-7 days'))
            ->getQuery();
Run Code Online (Sandbox Code Playgroud)