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

山姆

我的学说查询显然返回了错误的类型,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

实体:

<?php

 namespace App\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Gedmo\Mapping\Annotation;


/**
 * LoginAttempts
 *        @ORM\Entity(repositoryClass="App\Repository\LoginAttemptsRepository")
 * @ORM\Table(name="login_attempts")
 */
class LoginAttempts
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=100, nullable=false)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=100, nullable=false)
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(name="time", type="string", length=100, nullable=false)
 */
private $time;

public function getId(): ?int
{
    return $this->id;
}

public function getUsername(): ?string
{
    return $this->username;
}

public function setUsername(string $username): self
{
    $this->username = $username;

    return $this;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(string $email): self
{
    $this->email = $email;

    return $this;
}

public function getTime(): ?string
{
    return $this->time;
}

public function setTime(string $time): self
{
    $this->time = $time;

    return $this;
}


}
飞行

emailLoginAttempts实际上正在返回数组,LoginAttempts因此 PHP 给了您错误。修复将取决于您的实际逻辑:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Symfony 4 Doctrine 多个表连接在一个属性中

来自分类Dev

Symfony Doctrine Join 和 In 查询

来自分类Dev

Symfony2 / Doctrine联合查询

来自分类Dev

Symfony/Doctrine:按子属性过滤查询

来自分类Dev

Symfony 3:Doctrine:schema:update返回ConnexionException

来自分类Dev

Symfony 4 / Doctrine 2-获取实际对象而不是代理

来自分类Dev

Symfony 4 不使用 Doctrine ODM 补水对象

来自分类Dev

了解 Symfony 4 中的问题 Doctrine 2 ORM Query Builder

来自分类Dev

Symfony2:Doctrine订户抛出ContextErrorException-必须是Doctrine \ Common \ Persistence \ Event \ PreUpdateEventArgs的实例

来自分类Dev

在Symfony 1.4和Doctrine中使用一个查询插入多行

来自分类Dev

在Symfony 1.4和Doctrine中使用一个查询插入多行

来自分类Dev

返回数组,不是Doctrine查询中的对象-Symfony2

来自分类Dev

返回数组,不是来自Doctrine查询的对象-Symfony2

来自分类Dev

Symfony2 Doctrine2 UniqueEntity验证,指定一个值

来自分类Dev

Symfony - Redis 和 Doctrine

来自分类Dev

Symfony 2.8 Doctrine Fixtures

来自分类Dev

Doctrine/Symfony - 继承 - 关系

来自分类Dev

Symfony Doctrine 实体概述

来自分类Dev

教义存储库“ Doctrine \ ORM \ EntityRepository”必须实现Symfony \ Bridge \ Doctrine \ Security \ User \ UserLoaderInterface

来自分类Dev

__construct() 必须是 Doctrine\Common\Persistance\ObjectManager 的一个实例

来自分类Dev

在Doctrine DQL查询(Symfony2)中使用COLLATE

来自分类Dev

如何在Doctrine查询构建器(Symfony)中使用countDistinct

来自分类Dev

如何使用Doctrine在Symfony2中实现子查询?

来自分类Dev

在Doctrine DQL查询(Symfony2)中使用COLLATE

来自分类Dev

如何使用Doctrine和Symfony查询数据库数据

来自分类Dev

在Symfony Doctrine查询构建器中使用PostgreSQL NOT SIMILAR TO

来自分类Dev

Symfony 2 Doctrine ODM在现有字段上返回错误

来自分类Dev

Symfony / Doctrine - GroupBy 未返回所有结果

来自分类Dev

主键ID Doctrine Symfony2缺少值

Related 相关文章

  1. 1

    Symfony 4 Doctrine 多个表连接在一个属性中

  2. 2

    Symfony Doctrine Join 和 In 查询

  3. 3

    Symfony2 / Doctrine联合查询

  4. 4

    Symfony/Doctrine:按子属性过滤查询

  5. 5

    Symfony 3:Doctrine:schema:update返回ConnexionException

  6. 6

    Symfony 4 / Doctrine 2-获取实际对象而不是代理

  7. 7

    Symfony 4 不使用 Doctrine ODM 补水对象

  8. 8

    了解 Symfony 4 中的问题 Doctrine 2 ORM Query Builder

  9. 9

    Symfony2:Doctrine订户抛出ContextErrorException-必须是Doctrine \ Common \ Persistence \ Event \ PreUpdateEventArgs的实例

  10. 10

    在Symfony 1.4和Doctrine中使用一个查询插入多行

  11. 11

    在Symfony 1.4和Doctrine中使用一个查询插入多行

  12. 12

    返回数组,不是Doctrine查询中的对象-Symfony2

  13. 13

    返回数组,不是来自Doctrine查询的对象-Symfony2

  14. 14

    Symfony2 Doctrine2 UniqueEntity验证,指定一个值

  15. 15

    Symfony - Redis 和 Doctrine

  16. 16

    Symfony 2.8 Doctrine Fixtures

  17. 17

    Doctrine/Symfony - 继承 - 关系

  18. 18

    Symfony Doctrine 实体概述

  19. 19

    教义存储库“ Doctrine \ ORM \ EntityRepository”必须实现Symfony \ Bridge \ Doctrine \ Security \ User \ UserLoaderInterface

  20. 20

    __construct() 必须是 Doctrine\Common\Persistance\ObjectManager 的一个实例

  21. 21

    在Doctrine DQL查询(Symfony2)中使用COLLATE

  22. 22

    如何在Doctrine查询构建器(Symfony)中使用countDistinct

  23. 23

    如何使用Doctrine在Symfony2中实现子查询?

  24. 24

    在Doctrine DQL查询(Symfony2)中使用COLLATE

  25. 25

    如何使用Doctrine和Symfony查询数据库数据

  26. 26

    在Symfony Doctrine查询构建器中使用PostgreSQL NOT SIMILAR TO

  27. 27

    Symfony 2 Doctrine ODM在现有字段上返回错误

  28. 28

    Symfony / Doctrine - GroupBy 未返回所有结果

  29. 29

    主键ID Doctrine Symfony2缺少值

热门标签

归档