Symfony2自定义存储库类 [英] Symfony2 custom repository class
问题描述
我是symfony2的新手,我正在尝试创建自定义的存储库类,不能这样做。
这是我在做什么:
- 我向实体类添加了注释(MobilePhones)
@ ORM\Entity(repositoryClass =Maak\DefaultBundle\Entity\MobilePhonesRepository)
-
在MobilePhonesRepository中,我创建了一个名为
的自定义函数findAllMobilePhones()
- p>在控制器中,我使用以下函数调用函数:
$ em-> getRepository MaakDefaultBundle:MobilePhones') - > findAllMobilePhones();
但我得到未定义的方法findAllMobilePhones() code>,我已经清除缓存并尝试了相同的错误。什么是错误?
我的存储库类:
<?php
命名空间Maak\DefaultBundle\Entity;
使用Doctrine\ORM\EntityRepository;
class MobilePhonesRepository扩展EntityRepository
{
public function findAllMobilePhones()
{
return $ this-> getEntityManager() - > createQuery(' SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC') - > getResult();
}
}
给所有的人,为他们的时间。最后我发现了这个问题。
我决定调试整个项目,发现 repository-class
没有被设置在XMLDriver中,将在元数据中设置 customRepositoryName
。这是因为我正在使用XML映射,对于这个实体,我使用注释提供 repositoryClass
。
再次感谢: )
I am new to symfony2 and I am trying to create custom repository class and couldn' do it.
Here is what I am doing:
- I added annotation to entity class ( MobilePhones )
@ORM\Entity(repositoryClass="Maak\DefaultBundle\Entity\MobilePhonesRepository")
In MobilePhonesRepository I created my custom function named
findAllMobilePhones()
In controller I called function using:
$em->getRepository('MaakDefaultBundle:MobilePhones')->findAllMobilePhones();
but I get Undefined method findAllMobilePhones()
, I have cleared cache and tried but same error. What is wrong?
My repository class:
<?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();
}
}
Thanks to all guys for their time. Finally I have found the issue.
I decided to debug whole project and found repository-class
was not being set in XMLDriver that was going to set customRepositoryName
in metadata. It was because I was using XML mappings and for this entity I was providing repositoryClass
using Annotations.
Thank again :)
这篇关于Symfony2自定义存储库类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!