将 sql 转换为 dql symfony

Convert sql to dql symfony

我想将 sql 中的这个请求转换为 dql,需要一点帮助。

SELECT *, COUNT(*) AS nb_files 
FROM palettes 
    JOIN files_palettes ON palettes.id = files_palettes.palettes_id 
GROUP BY files_palettes.palettes_id 
ORDER BY nb_files DESC

编辑

尝试查询。

public function findSaveByPagesByFilters()
{
    $entityManager = $this->getEntityManager();

    $select = " SELECT  COUNT (p) AS nb_files";       
    $from = " FROM App\Entity\Palettes p ";
    $join = "JOIN p.files f ";
    $on = " ON p.id = f.palettes ";
    $groupBy = " GROUP BY f.p";
    $orderBy = " ORDER BY f.name DESC ";
       
    $dqlQuery = $select . $from  . $join . $on . $groupBy;
    //dd($dqlQuery);
    $query = $entityManager->createQuery(
        $dqlQuery
    );

  
    return $query->getResult();
} 

这是insomnia发来的消息:

[Syntax Error] line 0, col 74: Error: Expected end of string, got 'ON'

$dql = "SELECT p as palette, count(p.id) as cnt FROM App\Entity\Palettes p " .
    "JOIN p.files f GROUP BY p.id ORDER BY cnt DESC";
    ;

谢谢亚历克西斯!!

我假设您在存储库的上下文中,所以在这种情况下我建议使用 Doctrine Query Builder,它有助于简化您的代码流,并且可能会帮助您 SQL 以后的转化。

要回答这个具体问题,您可能需要执行以下操作:

public function findSaveByPagesByFilters()
{
    return $this->createQueryBuilder('p')
        ->innerJoin('p.files', 'f', Query\Expr\Join::ON, 'p.id = f.pallets')
        ->select(['*', 'count(p.id)'])
        ->groupBy('f.p')
        ->orderBy('f.name', 'DESC')
        ->getQuery()
        ->getResult();
}