的Symfony查询错误:预期的文字,得到“”

我有以下查询:

$query = $em->createQueryBuilder()
            ->select('u.id, u.username, u.username_canonical, u.email, u.email_canonical, u.last_login, u.name, u.type, u.phone, u.site, u.agency, u.subtype, u.info, u.created_date, u.vip')
            ->from('ContrateAdminBundle:Fosuser', 'u')
            ->where('u.created_date BETWEEN "'.$fromdateaccounts.'" AND "'.$todateaccounts.'" ')                      
            ->getQuery();

$results = $query->getResult();

在Symfony中获取用户数据,但是出现以下错误:

[Syntax Error] line 0, col 223: Error: Expected Literal, got '"'
500 Internal Server Error - QueryException
1 linked Exception: QueryException »

我的查询输出是:

SELECT u.id, u.username, u.username_canonical, u.email, u.email_canonical, u.last_login, u.name, u.type, u.phone, u.site, u.agency, u.subtype, u.info, u.created_date, u.vip
FROM ContrateAdminBundle:Fosuser u
WHERE u.created_date BETWEEN "2014-08-04 13:29" AND "2014-10-30 13:29" 
最佳答案
SQL中的字符串文字用单引号’表示,而不用双引号’表示,因此您必须在查询中将“作为字符串定界符替换为’:

$query = $em->createQueryBuilder()
            ->select('u.id, u.username, u.username_canonical, u.email, u.email_canonical, u.last_login, u.name, u.type, u.phone, u.site, u.agency, u.subtype, u.info, u.created_date,u.vip')
            ->from('ContrateAdminBundle:Fosuser', 'u')
            ->where('u.created_date BETWEEN \'${fromdateaccounts}\' AND \'{$todateaccounts}\'')                      
            ->getQuery();

转载注明原文:的Symfony查询错误:预期的文字,得到“” - 代码日志