Custom query from Symfony controller returns null

105 views Asked by At

I'm trying to build a custom query from symfony controller but it returns null. When I execute this query on phpMyadmin it returns results.

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

Could you please tell me what I'm doing wrong ? How can we write this on DQL ?

Thanks.

0

There are 0 answers