php - Symfony:无法启用自定义存储库

标签 php symfony doctrine-orm doctrine

我正在尝试为我的 symfony 2.8 网站使用自定义存储库:

自定义存储库:

// src/AppBundle/Entity/ExperimentationRepository
namespace AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ExperimentationRepository extends EntityRepository
{    
    public function getExperimentationByUser($id){
        // do stuff and return results
    }    
}

实体:

// src/AppBundle/Entity/Experimentation
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Experimentation
 *
 * @ORM\Table(name="experimentation")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ExperimentationRepository")
 */
class Experimentation{
   // sutff
}

Controller (我尝试在另一个包中使用自定义存储库的地方):

// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$experimentations = $em->getRepository('AppBundle:Experimentation')->getExperimentationByUser($id);

我收到以下错误:

Undefined method 'getExperimentationByUser'. The method name must start with either findBy or findOneBy!

经过一些研究,我尝试检查我的自定义存储库是否被调用:

$repository=$this->getDoctrine()
        ->getRepository('AppBundle:Experimentation');
        $repositoryClass=get_class($repository);
echo $repositoryClass;
exit;

哪个返回:

Doctrine\ORM\EntityRepository

所以我想我的自定义存储库根本没有被调用。但我找不到原因。我尝试了几种操作(经过一些挖掘):

  • 清除缓存(app/console cache:clear)
  • 清除 Doctrine 缓存(app/console Doctrine :cache:clear-metadata)
  • 检查 config.yml(在 doctrine:orm 下):auto_mapping: true
  • 检查 config.yml(在 doctrine:dbal 下):type: annotation
  • 我没有任何 config/doctrine 文件夹(这正常吗?)

没有任何效果。仍然有同样的错误。看起来框架完全忽略了实体类中的注释。有什么想法/建议吗?

感谢您的帮助!

最佳答案

你试过吗:

// src/ManageBundle/Controller/ManageController

$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:Experimentation');
$experimentations = $repository->getExperimentationByUser($id);

并生成实体

php app/console doctrine:generate:entities Experimentation

关于php - Symfony:无法启用自定义存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676617/

相关文章:

php - 在 PHP 中到处使用准备好的语句? (PDO)

php - 从android : ERROR : String cannot be converted to JSONArray读取mysql中的数据

php - 重复行 CodeIgniter

mysql - 如何让 Doctrine 2 以字符串形式返回 DateTime?

PHP Jquery JSON 捕获

model-view-controller - Symfony2/Doctrine,必须将业务逻辑放在我的 Controller 中吗?和复制 Controller ?

php - FOSOAuthServerBundle 和 FOSUserBundle - 如何让它工作?

symfony - Symfony/twig 中的分组复选框

php - 动态目标实体 Doctrine 2 和 Symfony 2

symfony - 学说2 - 如何使用 DATE_ADD 函数