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

时间:2016-01-08 11:58:38

标签: php symfony doctrine-orm doctrine

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

自定义存储库:

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
    <button data-bind="click: initData">Load data</button>
    <h4>Preview</h4>
    <p>
        <select data-bind="options: someOptions, value: myOption"></select>
        <br />
        <!-- ko if: !!myOption() -->
        A <span data-bind="text: myOption"></span>.
        <!-- /ko -->
    </p>
</div>

实体:

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

use Doctrine\ORM\EntityRepository;

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

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

// 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
}

我收到以下错误:

  

未定义的方法'getExperimentationByUser'。       方法名称必须以findBy或findOneBy!

开头

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

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

返回:

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

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

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

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

感谢您的帮助!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我终于设法让它发挥作用。在几个命令之后,我设法创建了AppBundle/Resources/config/doctrine file。它充满了.orm.xml个文件,所以我只是从doc:

中获取了XML语法
  <entity
     name="AppBundle\Entity\Experimentation"
     repository-class="AppBundle\Entity\ExperimentationRepository">

然而,我仍然不明白为什么注释系统不起作用,因为我已经非常努力地启用它。是否至少有一种方法可以检查启用了哪个系统(XML,YAML或Annotation)?如果您有线索,请随时发表评论......

答案 2 :(得分:0)

您只需删除缓存文件夹即可。使用CLI命令清除缓存对我不起作用。 php app/console cache:clear