我正在尝试从单个用户的过去3个月中检索所有Facture
,我想出了SQL方法,但我很难将此逻辑应用于查询构建器,我使用的是Symfony3-3。
$now = new Date('now');
$qb = $em->createQueryBuilder();
$qb->select('f')
->from('Facture', 'f')
->where('f.doDate BETWEEN :old AND :now')
->andwhere('f.userId =:id')
->setParameter('old', ($now-90))
->setParameter('now', $now);
->setParameter('id', $user->getId());
这是我想要实现的查询:
SELECT * FROM Facture WHERE (f.DO_Date BETWEEN CURDATE() - INTERVAL 90 DAY AND CURDATE()) AND f.clientId ='?someone's id?'
注意:我尝试过DQL但是我遇到的错误太多了。
答案 0 :(得分:2)
试试这个(我想你在你的Facture存储库中):
$qb = $em->createQueryBuilder('f');
$qb->andWhere(
$qb->expr()->between('f.doDate', ':startDate', ':endDate')
)
->andwhere('f.userId =:id')
->setParameter('startDate', new \DateTime('-90 days'))
->setParameter('endDate', new \DateTime());
->setParameter('id', $user->getId())
;