CreateQuery Doctrine Symfony 2 StateFieldPathExpression

时间:2013-11-21 09:47:27

标签: php mysql symfony doctrine-orm doctrine

我在MySql中有这个查询

SELECT l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
FROM sn_like_spotted l
LEFT JOIN prof_foto f
ON f.id = l.spotted_id
LEFT JOIN sn_profilo p
ON f.profilo_id = p.id
WHERE p.id = 3
GROUP BY l.spotted_id
ORDER BY numero_likes DESC LIMIT 0,10

我尝试在Doctrine

中执行此操作
public function getFoo($profilo_id){
    $em = $this->getEntityManager();
    $query = $em->createQuery('
    SELECT  l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
    FROM SNLikeBundle:LikeSpotted l
    LEFT JOIN SNFotoBundle:Foto f
    WITH f.id = l.spotted_id
    LEFT JOIN SNProfiloBundle:Profilo p
    WITH f.profilo_id = p.id
    WHERE p.id =  :profilo_id
    GROUP BY l.spotted_id
    ORDER BY numero_likes DESC
  ')->setFirstResults(0)
    ->setMaxResults(10)
    ->setParameter('profilo_id', $profilo_id);

    $results = $query->getResult();
    return $results;


}

然后我有这个错误

[Semantical Error] line 0, col 36 near 'spotted_id as': Error: Class SN\LikeBundle\Entity\LikeSpotted has no field or association named spotted_id

我改变了SELECT:

SELECT  l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes

我有这个错误:

[Semantical Error] line 0, col 36 near 'spotted as spotted_id,count(l.spotted)': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

然后我尝试使用IDENTITY

SELECT IDENTITY l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes

但我有这个错误

[Syntax Error] line 0, col 26: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'

我尝试在QueryBuilder中创建(更多标准,相同结果)

    $q = $this->createQueryBuilder('l');
    $q->leftJoin("l.spotted", 's');
    $q->leftJoin("s.profilo", 'p');
    $q->leftJoin("p.utente", 'u');
    $q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
    $q->andWhere('p.fase_registrazione = :fase');
    $q->andWhere('u.locked = :false');
    $q->andWhere('p.id = :profilo_id');
    $q->setParameter(':fase', 100);
    $q->setParameter('false', false);
    $q->setParameter('profilo_id', $profilo_id);
    $q->groupBy('l.spotted');
    $q->orderBy($q->expr()->count('l.spotted'), 'desc');
    $q->setMaxResults(10);
    $dql = $q->getQuery();
    $results = $dql->execute();

    return $results;

我的错误是

[Syntax Error] line 0, col 285: Error: Expected end of string, got '('

问题在于

$q->orderBy($q->expr()->count('l.spotted'), 'desc');

1 个答案:

答案 0 :(得分:0)

对于CreateBuilder案例我已解决:

 $q = $this->createQueryBuilder('l');
        $q->addSelect('count(l.spotted) as numero_likes');
        $q->leftJoin("l.spotted", 's');
        $q->leftJoin("s.profilo", 'p');
        $q->leftJoin("p.utente", 'u');
        $q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
        $q->andWhere('p.fase_registrazione = :fase');
        $q->andWhere('u.locked = :false');
        $q->andWhere('p.id = :profilo_id');
        $q->setParameter(':fase', 100);
        $q->setParameter('false', false);
        $q->setParameter('profilo_id', $profilo_id);
        $q->groupBy('l.spotted');
        $q->addOrderBy('numero_likes','DESC');
        $q->setMaxResults(10);
        $dql = $q->getQuery();
        $results = $dql->execute();

        return $results;

如果我不想在我的结果中使用numero_likes,我可以这样做:

    $q->addSelect('count(l.spotted) as HIDDEN numero_likes');

但我想如何在createQuery

中执行此操作