我尝试按照本教程进行分页:https://knpuniversity.com/screencast/symfony-rest3/pagerfanta-pagination
目前我安装了white-october/pagerfanta-bundle
,当我创建我的用户存储库时如下:
<?php
// src/UsersBundle/Repository/UserRepository.php
namespace UsersBundle\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository {
public function findAllQueryBuilder() {
return $this->createQueryBuilder('user');
}
}
在我的控制器动作中我这样做了:
<?php
// src/UsersBundle/Controller/UsersController.php
namespace UsersBundle\Controller;
// use statements ...
/**
* Class UsersController
*
* @package UsersBundle\Controller
*
* @Rest\RouteResource("user", pluralize=false)
* @Rest\NamePrefix( "api_v1_" )
*/
class UsersController extends FOSRestController implements ClassResourceInterface {
// ...
/**
* Responsible to list all the registered users.
*
* @Rest\QueryParam(name="page", default="1", requirements="\d+",nullable=true)
*
* @return array|\Traversable
*/
public function getListAction( ParamFetcher $param_fetcher ) {
if ( ! $this->isGranted( 'ROLE_ADMIN' ) ) {
throw new UnauthorizedHttpException( 'You do not have sufficient permission to access this resource' );
}
$qb = $this->getDoctrine()
->getRepository('UsersBundle\Repository\UserRepository')
->findAllQueryBuilder();
$adapter = new DoctrineORMAdapter( $qb );
$users = $this->user_manager->findUsers();
return $users;
}
// ...
}
我得到了这个输出:
{"code":500,"message":"The class 'UsersBundle\\Repository\\UserRepository' was not found in the chain configured namespaces UsersBundle\\Entity, FOS\\UserBundle\\Model"}
任何人都知道输出意味着什么?不幸的是,我对Symfony完全不熟悉,我无法理解这个输出的问题是什么。
答案 0 :(得分:3)
$qb = $this->getDoctrine()
->getRepository('UsersBundle\Repository\UserRepository')
->findAllQueryBuilder();
你应该通过你的实体找到你的存储库,所以像这样的
$qb = $this->getDoctrine()
->getRepository('User:class')
->findAllQueryBuilder();
Doc:https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database
在你的User
实体中你应该有类似的东西
/**
* @ORM\Table(name="User")
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User {
...