来自 Symfony 控制器的自定义查询返回 null

问题描述 投票:0回答:0

我正在尝试从 symfony 控制器构建自定义查询,但它返回 null。当我在 phpMyadmin 上执行此查询时,它会返回结果。

SyntheseController.php:

     * @Route("/synthese/categorie/{period}", name="categoriesSyntheseByMonthAndYear")
     * 
     */
    public function getCategorieSyntheseByMonthAndYear(String $period, Request $request): Response
    {
        $em = $this->getDoctrine()->getManager();

        $periodArray = explode("-",$period);

        $month = $periodArray[0];
        $year = $periodArray[1];

        $jsonData = [];

        $sql = "SELECT categories.nom_categorie, sum(depenses.montant_depense) AS montant "
            . "FROM sous_categories "
            . "LEFT JOIN depenses ON depenses.sous_categorie_id = sous_categories.id "
            . "LEFT JOIN categories ON sous_categories.categories_id = categories.id "
            . "WHERE categories.nom_categorie "
            . "IN (SELECT DISTINCT nom_categorie FROM categories WHERE 1) AND depenses.mois_depense = '" . $month . "' AND depenses.annee_depense = '" . $year . "' GROUP BY categories.nom_categorie;";

        $query = $em->createQuery($sql);
        $syntheseCategories = $query->getResult();

        var_dump($syntheseCategories);

        foreach($syntheseCategories as $syntheseCategorie){
            echo $syntheseCategorie['nom_categorie'];
            $jsonData[] = [
                'name' => $syntheseCategorie['nom_categorie'],//$syntheseCategories['nom_categorie'],
                'y' => (double) $syntheseCategories['montant'],
            ];
        }
        return new JsonResponse($jsonData); 
    }

你能告诉我我做错了什么吗? 我们如何在 DQL 上写这个?

谢谢。

symfony controller dql
© www.soinside.com 2019 - 2024. All rights reserved.