自定义存储库不起作用

时间:2017-07-21 11:28:16

标签: symfony doctrine

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:我试图清除缓存,但它没有效果:

Hett@Range 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.


Hett@Range MINGW64 /c/www/symfony (master)
$ ./bin/console doctrine:cache:clear-metadata
Clearing ALL Metadata cache entries
Successfully deleted cache entries.

enter image description here

1 个答案:

答案 0 :(得分:0)

我有自动生成的文件 src/AppBundle/Resources/config/doctrine/Users.orm.yml 据我所知.yml configs重写注释congiruation, 当我删除Users.org.yml或添加行repositoryClass: AppBundle\Repository\UsersRepository时 - 一切正常。 哪种方法是正确的?