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

3

我正在尝试为我的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
}

控制器(我试图在另一个包中使用自定义存储库):

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

我遇到了以下错误:
未定义方法 'getExperimentationByUser'。方法名必须以 findBy 或 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文件夹(这正常吗?)
什么都没用。仍然得到相同的错误。看起来框架完全忽略实体类中的注释。有什么想法/建议吗?
感谢您的帮助!

你生成了实体吗? - AnkiiG
相当神秘。您只定义了一个实体管理器吗?我怀疑有个简单的拼写错误。app/console doctrine:mapping:info是否可以获取到实体?是的,使用注释时将config/doctrine留空是正常的。config/doctrine通常用于存放yaml或xml映射文件。事实上,如果您在这里有文件,那么注释将被忽略。 - Cerad
实体已生成。app/console doctrine:mapping:info 使用“OK”标签选择所有实体。 - DavidL
尝试将您的实体注释更改为 ORM\Entity(repositoryClass="Test")。您应该会收到致命错误:Fatal error: Class 'Test' not found in... 如果您没有收到此错误,则说明注释未被正确读取。 - Wilt
我在这里瞎猜。您确定已编辑了正确的Experimentation.php文件吗?我有时会感到困惑,并编辑不同项目中的完全不同的文件,或忘记保存我的更改。您是否已经将您的代码提交到GitHub上了?如果您已经提交了,那我可以帮您看一下。问题肯定是简单的配置错误。 - Cerad
3个回答

0

你只需要删除缓存文件夹。使用CLI命令清除缓存对我没有用。

php app/console cache:clear

0
你试过这样吗:

// src/ManageBundle/Controller/ManageController

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

生成实体

php app/console doctrine:generate:entities Experimentation

是的,我尝试生成实体。一切看起来都很好,除了在加载页面时它不起作用。我尝试了你的代码,即使它与我的完全相同(你只是在中间添加了一个存储库变量);它也没有解决问题。无论如何,还是谢谢! - DavidL

0

我终于成功了。经过几个命令,我成功创建了AppBundle/Resources/config/doctrine file。它里面有很多.orm.xml文件,所以我只是从文档中取出了XML语法:

  <entity
     name="AppBundle\Entity\Experimentation"
     repository-class="AppBundle\Entity\ExperimentationRepository">

然而,我仍然不明白为什么注释系统没有起作用,因为我已经尝试了很多次启用它。是否至少有一种方法可以检查启用了哪个系统(XML、YAML或注释)?如果您有线索,请随意发表评论...


在你最初的问题中,你说你检查了config/doctrine吗?你也检查了AppBundle/Resources/config/doctrine吗?如果你在那里有文件,那就可以解释为什么注释被忽略了。 - Cerad
这就是发生的事情。如果我删除那些文件,注释会被考虑吗? - DavidL
看起来您是通过 shell 初始导入时没有使用注释格式自己创建了这些 XML 文件? - mblaettermann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接