我正在尝试按天和月订购查询结果,这是我的代码
public function getUserBirthday($idList)
{
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder->select('fb')
->from('Wishwish\ApplicationBundle\Entity\FbUser', 'fb')
->where(' Week(fb.birthday)<= Week(:now)+2')
->andWhere('Week(fb.birthday)> Week(:now)')
->andWhere('fb.id IN (:idList)')
->orderBy('DAY(fb.birthday)')
->addOrderBy('MONTH(fb.birthday)');
$queryBuilder->setParameter('now', new \DateTime());
$queryBuilder->setParameter('idList',$idList);
return $queryBuilder->getQuery()->getResult();
}
此ID显示错误:
[Syntax Error] line 0, col 170: Error: Expected end of string, got '('
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
update =&gt;解决方案: 诀窍是使用createNativeQuery
public function getUserBirthday($idList)
{
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Wishwish\ApplicationBundle\Entity\FbUser', 'fb');
$rsm->addFieldResult('fb', 'birthday', 'birthday');
$rsm->addFieldResult('fb', 'picture', 'picture');
$rsm->addFieldResult('fb', 'name', 'name');
$rsm->addFieldResult('fb', 'id', 'id');
$sql ='select * from fb_user
where (Week(birthday)<= Week(:now)+2) and (Week(birthday)> Week(:now))
and (id IN (:idList)) ORDER By DAY (birthday),MONTH (birthday)';
$query = $this->_em->createNativeQuery($sql, $rsm);
$query->setParameter('now', new \DateTime());
$query->setParameter('idList',$idList);
return $query->getResult();
}
答案 0 :(得分:0)
尝试使用createNativeQuery()方法进行更复杂的查询:
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping();
$query = $this->_em->createNativeQuery('
SELECT fb
FROM WishwishApplicationBundle:FbUser fb
WHERE Week(fb.birthday) <= Week(:now)+2
AND Week(fb.birthday) > Week(:now)
AND fb.id IN (:idList)
ORDER BY DAY(fb.birthday) ASC, MONTH(fb.birthday) ASC
', $rsm)
->setParameter('now', new \DateTime())
->setParameter('idList',$idList);
$result = $query->getResult();