我无法处理 find 函数的 null return
I can not handle the null return of the find function
正如题中所言,本人尽量按无效主义回应。然而,尽管转储确认 return 为空值,但我的代码并未考虑它。请问是什么原因呢?
这是我的代码:
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
你需要做这样的事情,因为你的函数 return 是一个集合而不是一个字符串:
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');
}
正如题中所言,本人尽量按无效主义回应。然而,尽管转储确认 return 为空值,但我的代码并未考虑它。请问是什么原因呢? 这是我的代码:
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
你需要做这样的事情,因为你的函数 return 是一个集合而不是一个字符串:
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');
}