7

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();
    }
}
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
Khalid Mushtaq
  • 103
  • 1
  • 7
  • 1
    What does `echo get_class($em->getRepository('MaakDefaultBundle:MobilePhones'))` return? Can you also provide the classfile for MobilePhonesRepository? – Sean Mar 02 '13 at 16:33
  • It returns Doctrine\ORM\EntityRepository – Khalid Mushtaq Mar 02 '13 at 16:56
  • getEntityManager() ->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC') ->getResult(); } } – Khalid Mushtaq Mar 02 '13 at 16:58
  • This is actually Doctrine 2 related, so you should tag your question with `doctrine2` to get max exposure. Also can you put your Repository code in the question so it's easily readable. – Bram Gerritsen Mar 02 '13 at 19:24
  • Are you 100% sure you have cleared the metadata cache? Your annotation is looking good and if `customRepositoryName` is set in your metadata there will be an exception if it cannot be instanciated correctly. If it is still not working put a `var_dump($metadata)` in the [EntityManager](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityManager.php) on line 750 and show us the output. – Bram Gerritsen Mar 02 '13 at 19:30
  • Try: $em->getRepository('Maak\DefaultBundle\Entity\MobilePhones')->findAllMobilePhones(); It's possible your bundle name is wrong or things are not configured quite right. – Cerad Mar 02 '13 at 20:29
  • @Bram : Yes I have cleared metadata cache. I have dumped metadata and it is not setting customRepositoryClassName.
    @Cerad : I have tried and it's not working either.
    – Khalid Mushtaq Mar 02 '13 at 20:44
  • @KhalidMushtaq, try putting some `var_dump` in the [AnnotationDriver](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php), in particular line 78. If you clear or disable the cache `loadMetadataForClass()` must be executed. – Bram Gerritsen Mar 02 '13 at 21:17
  • @BramGerritsen, I have cleared cache and placed var_dump in the AnnotationDriver but it is not being executed. – Khalid Mushtaq Mar 02 '13 at 21:37
  • @KhalidMushtaq, I'm pretty sure your metadata is still loaded from the cache. You really should **disable** all caching first, instead of invalidating it. – Bram Gerritsen Mar 02 '13 at 21:47
  • @BramGerritsen, I have disabled $kernel->loadClassCache() and I'm already using app_dev.php but it still doesn't work for me. – Khalid Mushtaq Mar 02 '13 at 21:58
  • @KhalidMushtaq, I'm afraid I can't help you any further because I am not a Symfony user. `loadClassCache()` sounds like classmap caching to me so that has nothing to do with Doctrine cache anyway. You tried [this](http://stackoverflow.com/questions/11826444/symfony2-doctrine-clear-cache). – Bram Gerritsen Mar 02 '13 at 22:05
  • @BramGerritsen, Thanks for your time. Yes I have done it. – Khalid Mushtaq Mar 02 '13 at 22:13

1 Answers1

2

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

Khalid Mushtaq
  • 103
  • 1
  • 7