【问题标题】:Symfony2 custom repository classSymfony2 自定义存储库类
【发布时间】:2013-02-17 01:22:50
【问题描述】:

我是 symfony2 的新手,我正在尝试创建自定义存储库类但无法做到。

这是我正在做的事情:

  • 我在实体类 (MobilePhones) 中添加了注释

@ORM\Entity(repositoryClass="Maak\DefaultBundle\Entity\MobilePhonesRepository")

  • 在 MobilePhonesRepository 中,我创建了名为 findAllMobilePhones() 的自定义函数

  • 在控制器中我使用以下方法调用函数:

$em->getRepository('MaakDefaultBundle:MobilePhones')->findAllMobilePhones();

但是我得到了Undefined method findAllMobilePhones(),我已经清除了缓存并尝试了同样的错误。怎么了?

我的存储库类:

<?php

namespace Maak\DefaultBundle\Entity;
use Doctrine\ORM\EntityRepository;

class MobilePhonesRepository extends EntityRepository
{
    public function findAllMobilePhones()
    {
        return $this->getEntityManager()->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC') ->getResult();
    }
}

【问题讨论】:

  • echo get_class($em-&gt;getRepository('MaakDefaultBundle:MobilePhones')) 返回什么?能否也提供 MobilePhonesRepository 的类文件?
  • 返回 Doctrine\ORM\EntityRepository
  • getEntityManager() ->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC') ->getResult(); } }
  • 这实际上与 Doctrine 2 相关,因此您应该使用doctrine2 标记您的问题以获得最大曝光率。您也可以将您的存储库代码放在问题中,以便于阅读。
  • 您是否 100% 确定已清除元数据缓存?您的注释看起来不错,如果在元数据中设置了 customRepositoryName,如果无法正确实例化,则会出现异常。如果仍然无法正常工作,请在第 750 行的 EntityManager 中添加 var_dump($metadata) 并向我们展示输出。

标签: php symfony doctrine-orm repository entity


【解决方案1】:

感谢所有人的宝贵时间。终于找到问题了。

我决定调试整个项目,发现 repository-class 没有在 XMLDriver 中设置,而 XMLDriver 将在元数据中设置 customRepositoryName。这是因为我使用的是 XML 映射,并且对于这个实体,我使用注释提供了 repositoryClass

再次感谢:)

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2012-11-14
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多