Symfony2 custom repository class

醉酒当歌 提交于 2019-12-01 15:25:28

问题


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();
    }
}

回答1:


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 :)



来源:https://stackoverflow.com/questions/15176424/symfony2-custom-repository-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!