Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 custom repository class

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();
    }
}
like image 735
Khalid Mushtaq Avatar asked Mar 02 '13 15:03

Khalid Mushtaq


People also ask

How do I create an entity repository?

Enter a class or namespace to regenerate [App\Entity]: Just press Enter or specify the location of your entity folder, and it will create missing getters/setters & Repositories.

What is a repository in Symfony?

A repository is a way to retrieve entities, so put on repositories any method you need to get them, such as getUserByEmail or whatever.

What is Doctrine in Symfony?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

What is entity Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.


1 Answers

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

like image 185
Khalid Mushtaq Avatar answered Sep 29 '22 19:09

Khalid Mushtaq