0

In symfony 5, with doctrine I want to create a request to select distinct type. So, I have made the following code:

 /**
     * @Route("/hometest", name="hometest")
     */
    public function index2()
    {
        $lesservices = $this->getDoctrine()->getRepository(Services::class)->findAll();
        $em = $this->getdoctrine()->getManager();
        $query = $em->createQuery('SELECT distinct serv.souscategorie FROM App\Entity\Services serv');
        $services = $query->getResult();
        return $this->render('home/indextest.html.twig', array ('servicesliste' => $lesservices, 'distinctscat' => $query));
    }

But, I am getting this error :

[Semantical Error] line 0, col 21 near 'souscategorie': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

In fact, souscategorie is a relation field and if I put a string field at his place everything goes right.

Do I have to make a join request to have my souscategorie results ?

Thanks for your answers.

zizoujab
  • 7,603
  • 8
  • 41
  • 72

1 Answers1

0

You are selecting a relation field, this might be your problem.

By searching your error i found out this answer which could help you solve your query without having to join:

SELECT DISTINCT IDENTITY(serv.souscategorie) ...
Agnohendrix
  • 480
  • 1
  • 7
  • 17