我是 symfony2 的新手,我正在尝试创建自定义存储库类,但无法做到。
这是我正在做的:
- 我为实体类 ( MobilePhones ) 添加了注解
@ORM\Entity(repositoryClass="Maak\DefaultBundle\Entity\MobilePhonesRepository")
在 MobilePhonesRepository 中,我创建了名为
findAllMobilePhones()
的自定义函数
在 Controller 中我调用了函数:
$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();
}
}
最佳答案
感谢大家的宝贵时间。终于找到问题了。
我决定调试整个项目,发现 repository-class
没有在 XMLDriver 中设置,而 XMLDriver 将在元数据中设置 customRepositoryName
。这是因为我使用的是 XML 映射,并且为此实体我使用注释提供了 repositoryClass
。
再次感谢:)
关于php - Symfony2 自定义存储库类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176424/