【发布时间】: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