DevEnStage
DevEnStage

Reputation: 41

I can not handle the null return of the find function

as announced in the title, I try to act according to the null doctrine response. However despite the dump which confirms the return of a null value, my code does not take it into account. Would you like what is the cause? This is my code :

    public function getUserBATS(UserRepository $repository ,$email, ObjectManager $em): Collection
    {
        $user = $repository->findOneBy(array('email' => $email));
        dump($user);
        if($user != null) {
            $bats = $user->getBATS();
            return $bats;
        }
        else
        { return $message = 'email don't...';}
    }

Symfony response

Upvotes: 0

Views: 52

Answers (1)

ElliotBrl
ElliotBrl

Reputation: 316

You need to do something like this cause your function return a Collection and not a string:

public function getUserBATS(UserRepository $repository ,$email, ObjectManager $em): Collection
{
    $user = $repository->findOneBy(array('email' => $email));
    if($user !== null) {
        return $user->getBATS();
    }
    throw new NotFoundHttpException('user not found');
}

Upvotes: 1

Related Questions