我使用Doctrine2 ORM进行了Silex设置。我正在尝试建立可用于实体的分页类。我非常了解Doctrine2中现有的分页课程,但是由于该项目是为我的学校研究而设计的,因此我尝试自己创建此组件。

以下是访问此页面时出现的致命错误:


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


我用两个方法count和paginate定义了一个名为PaginateableInterface的接口。我继续定义了一个自定义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 - Doctrine2实体管理器在 namespace 中找不到自定义存储库类-LMLPHP

最佳答案

您两次声明@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

07-24 09:49
查看更多