【问题标题】:Doctrine2 Entity Manager cannot find Custom Repository Class in namespaceDoctrine2 实体管理器在命名空间中找不到自定义存储库类
【发布时间】:2020-06-04 02:45:52
【问题描述】:

我使用 Doctrine2 ORM 设置了 Silex。我正在尝试构建一个可以与我的实体一起使用的分页类。我很清楚 Doctrine2 中存在的现有分页类,但因为这个项目是为了我的学校研究,我正在尝试自己创建这个组件。

以下是我在访问此页面时遇到的致命错误:

致命错误:第 689 行的 D:\web\playground-solutions\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php 中找不到类“PlayGround\Model\Helper\UserRepository”

我定义了一个名为 PaginateableInterface 的接口,其中包含两个方法 count 和 paginate。我接着定义了一个自定义的 EntityRepository 类,它扩展了 Doctrine\ORM\EntityRepository。下面是我的自定义 EntityRepository。

<?php

 namespace PlayGround\Service\Doctrine;

 use Doctrine\ORM\EntityRepository as ParentEntityRepository;

 class EntityRepository extends ParentEntityRepository{

   public function count(){

      $em       = $this->getEntityManager();

      $builder  = $em->createQueryBuilder();
      /**
      * ToDo: @entity
      *
      * Still need to find a better way of getting entity class name.
      */
      $entity   = $em->getClassMetadata(get_class(__CLASS__))->getName();

      //Dynamically get a count of records on any entity we happen to call this on.
      $builder->select($builder->expr()->count('e'))
       ->from($entity, 'e');

      $query   = $builder->getQuery();

      //Try-Catch block ommitted
      return $query->getSingleScalarResult();
    }
  }

 <?php

   namespace PlayGround\Model\Helper;

   use PlayGround\Service\Doctrine\EntityRepository as CustomRepository;
   use PlayGround\Contract\PaginateableInterface as IPaginate;

   class UserRepository extends CustomRepository  implements IPaginate
   {
    }

据我了解,这应该足够了,因为 count 和 paginate 方法位于自定义存储库中。

在我的 Paginator 类中,我调用要分页的实体,如下所示:

<?php

  //Paginator class
  $model = $this->getModel($model);
  //Count should be inherited from CustomRepository aliased object.
  $totalRecords = $model->count(); 

下面是关于这个的另一个突破点,我在我的模型中添加一个注释以将它指向它应该使用的存储库类。

<?php
  namespace Application\Model\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Application\Model\Entity\UserGroup;


 /**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity(repositoryClass="PlayGround\Model\Helper\UserRepository")
 */
 class User{ /* Rest of the code goes here... */ }

考虑到所有这些设置,我在让它工作时会错过什么?我什至在我的理论控制台上运行了两个命令,但这也没有帮助。

Luyanda.Siko@ZACT-PC301 MINGW64 /d/web/playground-solutions
$ php app/Console/bin/doctrine.php orm:clear-cache:metadata
  Clearing ALL Metadata cache entries
  Successfully deleted cache entries.

Luyanda.Siko@ZACT-PC301 MINGW64 /d/web/playground-solutions
$ php app/Console/bin/doctrine.php orm:clear-cache:query
  Clearing ALL Query cache entries
  Successfully deleted cache entries.

编辑:

下面是我在 D:\web\playground-solutions 中找到的文件结构。

【问题讨论】:

  • 你用来存储类的路径是什么?

标签: php symfony doctrine-orm pagination


【解决方案1】:

你声明了两次@ORM\Entity。一次使用repositoryClass,一次没有。删除没有的:

@ORM\Entity

然后离开:

@ORM\Entity(repositoryClass="PlayGround\Model\Helper\UserRepository")

@ORM\HasLifecycleCallbacks 声明时应不带括号 ()...

还要确保EntityRepository 位于正确的命名空间和相应的文件夹中:

你的命名空间是PlayGround\Model\Helper\UserRepository,这意味着文件应该在文件夹PlayGround\Model\Helper中,类文件名应该是UserRepository.php

修复并检查它,如果它仍然不起作用,请发表评论。

更新:

您的 UserRepository 位于错误的模块中。现在在app 应该在PlayGround

【讨论】:

  • 谢谢@Wilt。我刚刚应用了您的建议,但仍然遇到同样的致命错误。我已将我的文件结构包含在我的问题的编辑部分中。提前致谢。
  • @LuyandaSiko 阅读我的更新...您遇到了命名空间问题。
  • 感谢@Witt 让我的大脑起死回生。我解决了。
【解决方案2】:

文件应该在:

src/PlayGround/Model/Helper/UserRepository.php

都是关于那个问题的。

【讨论】:

    【解决方案3】:

    显然,这确实消耗了我的思考过程。正如@Witt 所指出的那样,我所做的只是指向一个不正确的命名空间。

    我更改了用户实体中的注释条目,错误消失了。

     <?php
       /** @ORM\Entity(repositoryClass="Application\Model\Helper\UserRepository") */
    

    谢谢你们。

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 2012-09-25
      • 1970-01-01
      • 2015-09-15
      • 2015-02-02
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多