'How to fetch a particular field from Symfony repository query result

enter image description here

Hello, I made a query in the repository Library and i am getting this result. From that i need to fetch the "code" of each of the users. Then i will load the resulting file in the form in order to build a "choice_type" with the codes. Is there a simple way to get these codes. Thank you.

Code for the query:

public function findUCodesByLibrairy($value)
    {
        return $this->createQueryBuilder('l')
        ->andWhere('u.librairy = :id')
        ->addSelect('u')
        ->leftJoin('l.users', 'u')
        ->setParameter('id', $value)
        ->getQuery()
        ->getResult();
        
    }


Solution 1:[1]

You almost did as you need, try to request not all properties, but a specific one as shown in the example.

public function findUCodesByLibrairy($value)
    {
        return $this->createQueryBuilder('l')
        ->andWhere('u.librairy = :id')
        ->Select('u.code') // hier we are requesting a specific property
        ->leftJoin('l.users', 'u')
        ->setParameter('id', $value)
        ->getQuery()
        ->getResult();
        
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harvey Dent