Symfony的3.3, 我根據文檔做https://symfony.com/doc/current/doctrine/repository.html自定義庫不起作用
$entityManager = $this->getDoctrine()->getManager();
$users = $entityManager->getRepository(Users::class)->findAllOrderedByName();
我得到異常:
BadMethodCallException:
Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy!
at vendor\doctrine\orm\lib\Doctrine\ORM\EntityRepository.php:226
at Doctrine\ORM\EntityRepository->__call('findAllOrderedByName', array())
(src\AppBundle\Controller\DefaultController.php:28)
at Doctrine\ORM\EntityRepository->findAllOrderedByName()
(src\AppBundle\Controller\DefaultController.php:28)
at AppBundle\Controller\DefaultController->indexAction(object(Request))
at call_user_func_array(array(object(DefaultController), 'indexAction'), array(object(Request)))
(var\cache\dev\classes.php:4453)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(var\cache\dev\classes.php:4408)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(web\app_dev.php:29)
用戶實體:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UsersRepository")
*/
class Users
{
...
}
UsersRepository
namespace AppBundle\Repository;
use AppBundle\Entity\Users;
use Doctrine\ORM\EntityRepository;
/**
* Created by PhpStorm.
* User: Hett
* Date: 21.07.2017
* Time: 14:25
*/
class UsersRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQueryBuilder()
->select("u")
->from(Users::class, "u")
->orderBy("name")
->getQuery()
->getArrayResult();
}
}
據我所知$entityManager->getRepository(Users::class)
返回默認的EntiryRepository。爲什麼?我的錯誤是什麼?
UPD:我試圖清除緩存,但它沒有任何效果:
[email protected] MINGW64 /c/www/symfony (master)
$ ./bin/console cache:clear
// Clearing the cache for the dev environment with debug
// true
[WARNING] Calling cache:clear without the --no-warmup option is deprecated
since version 3.3. Cache warmup should be done with the cache:warmup
command instead.
[OK] Cache for the "dev" environment (debug=true) was successfully cleared.
[email protected] MINGW64 /c/www/symfony (master)
$ ./bin/console doctrine:cache:clear-metadata
Clearing ALL Metadata cache entries
Successfully deleted cache entries.
通常它是存儲庫映射問題的實體。確保在Resources/config/doctrine下沒有任何舊的映射文件。確保清除緩存。有時候,在滿月的時候唱Hey Hey也會有幫助。 – Cerad
@Cerad已經嘗試,但它不能解決問題(我更新了我的文章) – Hett
你有多個實體經理嗎? (爲了清晰起見:-)) –