我在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


我试图在学说中做到这一点

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.


然后我尝试身份

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');

最佳答案

对于我解决的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中做到这一点

关于php - CreateQuery Doctrine Symfony 2 StateFieldPathExpression,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20117419/

10-09 22:09