1

我在 Symfony2 中将值传递给 Doctrine2 QueryBuilder 时遇到问题。我有两个实体加入了单向多对多关系:文章和标签,它工作正常,但是当我尝试将值传递给 qb 参数时,它读取错误 espetcted Literal got 'ORDER' 或一无所获(但记录存在)。编码:

$em=$this->getDoctrine()->getEntityManager()->getRepository('Bundle:Articles');
$qb = $em->createQueryBuilder('l')
        ->select('l,t')
        ->leftJoin('l.tags', 't')
        ->where('1=1')
        ->orderBy('l.titol', 'ASC');

if(isset($criteria['Titol']) && !empty($criteria['Titol']) &&
    strlen(trim($criteria['Titol']))){
        $qb->andWhere('l.titol like :titol')
           ->setParameter('titol','%'.$criteria['Titol'].'%');
}

if(isset($criteria['includeRecurs']) && 
    !empty($criteria['includeRecurs']) && 
    trim($criteria['includeRecurs']=='recurs')){
        $qb->andWhere('trim(l.urlMicroSite) !=\'\' and l.urlMicroSite is not null');
}

if(isset($criteria['tag']) && is_array($criteria['tag']) && count($criteria['tag'])){
    foreach($criteria['tag'] as $k=>$v){
        $qb->andWhere( 't.id=:'.$k)->setParameter($k,$criteria['tag'][$k]);
    }
}

问题出在最后一个作业中,其他作品:

$qb->andWhere( 't.id=:'.$k)->setParameter($k,$v);

如果我手动设置变量 $v 例如: setParameter($k,3) 或 setParameter($k,'3') 它工作正常......但如果我设置值 $v=3 然后调用代码,它没有得到任何东西(也没有错误)。如果我直接设置 andWhere 条件,例如 andWhere('t.id='.$v) 它会读取 Error especting Literal got 'ORDER',硬编码它再次起作用的值。我确定 $v 是正确的(var_dump、echo 等)。任何帮助将不胜感激(对不起我的英语)

4

1 回答 1

0

好吧,由于帖子空间,我需要在这里发布:案例 1 使用原始参数查询,没有结果:

 --no results (parameter $v)
    SELECT l0_.ISBN AS ISBN0, l0_.EAN AS EAN1, l0_.numeroPagines AS numeroPagines2,
    l0_.preuLlibre AS preuLlibre3, 
    l0_.format AS format4, 
    l0_.dataPublicacio AS dataPublicacio5, 
    l0_.titolOriginal AS titolOriginal6, 
    l0_.disenyCoberta AS disenyCoberta7,
    l0_.ilustrador AS ilustrador8, l0_.coleccio AS coleccio9, l0_.versioDe AS versioDe10,
    l0_.descripcioCurta AS descripcioCurta11, l0_.urlMicroSite AS urlMicroSite12,
    l0_.visibleInici AS visibleInici13, l0_.titol AS titol14, l0_.autor AS autor15,
    l0_.descripcio AS descripcio16, l0_.imatge AS imatge17, l0_.id AS id18,
    l0_.idUsuariCreacio AS idUsuariCreacio19, l0_.dataCreacio AS dataCreacio20,
    l0_.idUsuariModificacio AS idUsuariModificacio21, 
    l0_.dataModificacio AS dataModificacio22,
    l0_.userLock AS userLock23, 
    t1_.nomTag AS nomTag24, 
    t1_.tipusTag AS tipusTag25, 
    t1_.id AS id26, 
    t1_.idUsuariCreacio AS idUsuariCreacio27, 
    t1_.dataCreacio AS dataCreacio28, 
    t1_.idUsuariModificacio AS idUsuariModificacio29, 
    t1_.dataModificacio AS dataModificacio30,
    t1_.userLock AS userLock31 
    FROM Llibre l0_ LEFT JOIN llibre_tag l2_ 
        ON l0_.id = l2_.llibre_id 
    LEFT JOIN Tags t1_ 
        ON t1_.id = l2_.tag_id 
    WHERE 1 = 1 
        AND t1_.id = '3' 
        **AND t1_.id = ''** <-- empty duplicated parameter (but why duplicated??)
    ORDER BY l0_.titol ASC

这里使用参数 $v=3 硬编码的相同查询

--1 result as expected (parameter $v)
    SELECT l0_.ISBN AS ISBN0, l0_.EAN AS EAN1, l0_.numeroPagines AS numeroPagines2,
    l0_.preuLlibre AS preuLlibre3, 
    l0_.format AS format4, 
    l0_.dataPublicacio AS dataPublicacio5, 
    l0_.titolOriginal AS titolOriginal6, 
    l0_.disenyCoberta AS disenyCoberta7,
    l0_.ilustrador AS ilustrador8, l0_.coleccio AS coleccio9, l0_.versioDe AS versioDe10,
    l0_.descripcioCurta AS descripcioCurta11, l0_.urlMicroSite AS urlMicroSite12,
    l0_.visibleInici AS visibleInici13, l0_.titol AS titol14, l0_.autor AS autor15,
    l0_.descripcio AS descripcio16, l0_.imatge AS imatge17, l0_.id AS id18,
    l0_.idUsuariCreacio AS idUsuariCreacio19, l0_.dataCreacio AS dataCreacio20,
    l0_.idUsuariModificacio AS idUsuariModificacio21, 
    l0_.dataModificacio AS dataModificacio22,
    l0_.userLock AS userLock23, 
    t1_.nomTag AS nomTag24, 
    t1_.tipusTag AS tipusTag25, 
    t1_.id AS id26, 
    t1_.idUsuariCreacio AS idUsuariCreacio27, 
    t1_.dataCreacio AS dataCreacio28, 
    t1_.idUsuariModificacio AS idUsuariModificacio29, 
    t1_.dataModificacio AS dataModificacio30,
    t1_.userLock AS userLock31 
    FROM Llibre l0_ LEFT JOIN llibre_tag l2_ 
        ON l0_.id = l2_.llibre_id 
    LEFT JOIN Tags t1_ 
        ON t1_.id = l2_.tag_id 
    WHERE 1 = 1 
        AND t1_.id = '3' 
        **AND t1_.id = '3'** 
    ORDER BY l0_.titol ASC

最后查询具有不重复的其他参数..

--1 result as expected (parameter $v=4,$title="%quil%")
SELECT l0_.ISBN AS ISBN0, l0_.EAN AS EAN1, l0_.numeroPagines AS numeroPagines2,
l0_.preuLlibre AS preuLlibre3, 
l0_.format AS format4, 
l0_.dataPublicacio AS dataPublicacio5, 
l0_.titolOriginal AS titolOriginal6, 
l0_.disenyCoberta AS disenyCoberta7,
l0_.ilustrador AS ilustrador8, l0_.coleccio AS coleccio9, l0_.versioDe AS versioDe10,
l0_.descripcioCurta AS descripcioCurta11, l0_.urlMicroSite AS urlMicroSite12,
l0_.visibleInici AS visibleInici13, l0_.titol AS titol14, l0_.autor AS autor15,
l0_.descripcio AS descripcio16, l0_.imatge AS imatge17, l0_.id AS id18,
l0_.idUsuariCreacio AS idUsuariCreacio19, l0_.dataCreacio AS dataCreacio20,
l0_.idUsuariModificacio AS idUsuariModificacio21, 
l0_.dataModificacio AS dataModificacio22,
l0_.userLock AS userLock23, 
t1_.nomTag AS nomTag24, 
t1_.tipusTag AS tipusTag25, 
t1_.id AS id26, 
t1_.idUsuariCreacio AS idUsuariCreacio27, 
t1_.dataCreacio AS dataCreacio28, 
t1_.idUsuariModificacio AS idUsuariModificacio29, 
t1_.dataModificacio AS dataModificacio30,
t1_.userLock AS userLock31 
FROM Llibre l0_ LEFT JOIN llibre_tag l2_ 
    ON l0_.id = l2_.llibre_id 
LEFT JOIN Tags t1_ 
    ON t1_.id = l2_.tag_id 
WHERE 1 = 1 
    AND l0_.titol LIKE '%quil%' 
    AND t1_.id = '3' 
    **AND t1_.id = '3**' 
ORDER BY l0_.titol ASC
于 2013-02-27T10:01:16.930 回答