Symfony 4 / Doctrine查询:返回值必须是的实例

时间:2018-08-13 14:36:06

标签: symfony doctrine-orm

我的学说查询显然在checkmate::assert_numeric(1, names = "named") #> Error in withCallingHandlers({: Assertion on '1' failed: Vector must be named. 返回了错误的类型,但是我不明白为什么。它说返回数组;这就是我所期望的...

控制器:

getResult()

存储库

public function checkbrute($username, $email ) {

   $repository = $this->getDoctrine()->getRepository(LoginAttempts::class);

   $now = time();
   $valid_attempts = $now - (2 * 60 * 60);

   $attempts = $repository->emailLoginAttempts($email, $valid_attempts);

   return sizeof($attempts);

}

错误:

    public function emailLoginAttempts($email, $valid_attempts): ?LoginAttempts
{
    return $this->createQueryBuilder('l')
        ->select('l.time')
        ->andWhere('l.email = :val')
        ->andWhere('l.time > :val2')
        ->setParameter('val', $email)
        ->setParameter('val2', $valid_attempts)
        ->getQuery()
        ->getResult() //ERROR HERE
    ;
}

实体:

Return value of App\Repository\LoginAttemptsRepository::emailLoginAttempts() must be an instance of App\Entity\LoginAttempts or null, array returned

1 个答案:

答案 0 :(得分:2)

您的emailLoginAttempts实际上是返回LoginAttempts的数组,因此,此PHP给您错误。解决方法取决于您的实际逻辑:

  1. 如果您需要从LoginAttempts接收单个emailLoginAttempts实例-您需要将getResult()替换为getOneOrNullResult()
  2. 如果您需要接收包含多个LoginAttempts实例的数组-您需要更新方法签名以返回数组:public function emailLoginAttempts($email, $valid_attempts): array并添加PHPDoc @return LoginAttempts[],这样类型信息就不会丢失。