Skip to content
Advertisement

Get data from multiple rows

I would like to get the value of the column uid from all rows where the value of the column city is london.

I have tried it like this:

$this->getDoctrine()->getRepository(Personal::class)->findOneBy(['city' => 'london']);

but then I get an object from only 1 row. What I need is simply an array that looks kind of like this:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

Advertisement

Answer

Use findBy instead findOneBy. It gives you array of entities.

But you can`t define, which column should be fetched. For that you should use DQL where you define what should be fetched.

For example with query builder:

$queryBuilder = $this->createQueryBuilder()
    ->select('p.uid')
    ->from(Personal::class, 'p')
    ->where('p.city = 'london'');

$uids = $queryBuilder->getQuery()->getResult();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement