V
V
Vitaliy2021-08-02 08:59:09
Doctrine ORM
Vitaliy, 2021-08-02 08:59:09

How to return empty array instead of null using Doctrine ORM without using Query Builder?

There is an original method

public function getUserData(string $email): array
    {
        return $this->findOneBy(['email' => $email]);
    }


If the user does not exist, null is returned, not an empty array.
How to get an empty array instead of null if the user's email key doesn't exist?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy, 2021-08-02
@inajaf

Added an additional condition and the issue is resolved.

public function getUserData(string $email): array
    {
        $query = $this->findOneBy(['email' => $email]);
        return $query ?? [];
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question