2017-07-21 54 views
1

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. 

enter image description here

+1

通常它是存儲庫映射問題的實體。確保在Resources/config/doctrine下沒有任何舊的映射文件。確保清除緩存。有時候,在滿月的時候唱Hey Hey也會有幫助。 – Cerad

+0

@Cerad已經嘗試,但它不能解決問題(我更新了我的文章) – Hett

+0

你有多個實體經理嗎? (爲了清晰起見:-)) –

回答

0

我有自動生成的文件 src/AppBundle/Resources/config/doctrine/Users.orm.yml 據我瞭解.yml CONFIGS改寫註釋congiruation, 當我刪除Users.org.yml或添加行repositoryClass: AppBundle\Repository\UsersRepository - 一切正常。 哪種方法是正確的?

+1

你可以一個或另一個(YML或註釋),但不能兩個。如果你喜歡註釋,那麼只需刪除yml文件。如果你不喜歡註釋,那麼保存yml文件。你的選擇。 – Cerad