31

I'm new in symfony2.I created a repository class when I created an entity class through command line.But I couldn't access my custom functions in that repository class. how to create custom repository class in symfony2? Can anybody give me a step by step explanation from scratch with some sample code?

Below is my repository class

namespace Mypro\symBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * RegisterRepository
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class RegisterRepository extends EntityRepository
{


    public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery('SELECT p FROM symBundle:Register p ORDER BY p.name ASC')
            ->getResult();
    }


}

I called in my controller like this way

$em = $this->getDoctrine()->getEntityManager();
          $pro = $em->getRepository('symBundle:Register')
            ->findAllOrderedByName();

I got the below error

Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy!

Do I have any mistake in my code ? Any mistake in creating repository class? did i need to use any class.

crmpicco
  • 16,605
  • 26
  • 134
  • 210
Asish AP
  • 4,421
  • 2
  • 28
  • 50

6 Answers6

69

I think you just forgot to register this repository in your entity. You just have to add in your entity configuration file the repository class.

In src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml:

Mypro\symBundle\Entity\Register:
    type: entity
    repositoryClass: Mypro\symBundle\Entity\RegisterRepository

Don't forget to clear your cache after this change, just in case.

And if you're using Annotations (instead of yml config) then instead of the above, add something like:

/**
 * @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository")
*/

to your Entity class to register the repository

Community
  • 1
  • 1
Reuven
  • 3,336
  • 2
  • 23
  • 29
  • Reuven I got the correct output,but in my Netbeans ide couldn't detect my custom repository function.y? do you know it? – Asish AP Nov 17 '11 at 05:10
  • I don't think it's possible for netbean to know the custom repository. My version of netbean does not autocomplete symfony specific code. – Reuven Nov 17 '11 at 06:56
  • I have the same problem and I did generate the EntityRepository with Symfony... but I don't have any folder called "doctrine" in src/Projet/Bundle/Resources/config/... what's wrong ? – httpete Jan 21 '12 at 17:22
  • me too. i also dont have the doctrine directory.. do i have to create it first and then create the mentioned file ? –  Feb 23 '12 at 12:17
  • this is the syntax for yml entity configuration. For annotation, please refer to the doctrine documentation – Reuven Feb 23 '12 at 15:45
  • same goes for xml: if you have ..../doctrine/Product.orm.xml add repository-class="your\Bundle\Entity\ProductRepository" into the tag –  Sep 16 '13 at 21:29
6

The manual has a nice step by step guide ... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

  • First define the repository class in the annotations / yaml configuration
  • Create the class
  • Create the function
  • Then call the newly created function ....
Manse
  • 37,765
  • 10
  • 83
  • 108
  • The example from the manaul does not work with Symfony 2.1.1, unless you change the annotation from `@ORM\Entity(repositoryClass="Acme\StoreBundle\Repository\ProductRepository")` to something like `@ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")` that starts with the namespace for the Entity\Product.php file – yitwail Sep 17 '12 at 06:37
4

I too lost a lot of time when I got into this mess of configuration. I was configuring the repository class in my Entity as annotation mapping. The mapping was there but still the repository was not associated with the entity. When I moved the annotation mapping i.e. @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository"), to the last line, it worked.

 /*
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository")
 */
class User
{
...
}`   
mansoor.khan
  • 2,309
  • 26
  • 39
  • Weird, that also worked for me after spending the last 2 hours trying to work out what the issue was. – Ben_hawk Jul 20 '13 at 13:12
4

xml for mapping: Update xml mapping file in folder Resource/config/doctrine, add repository-class attribute:

<entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository">

http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/mapping-classes-to-orm-and-odm.html Then update cache:

php app/console doctrine:cache:clear-metadata
php app/console cache:clear
Peniel1127
  • 73
  • 7
2

First of all You don't need custom repo to do that.. You can set the order by clause in the EM getRepository findBy method:

//$em - entity manager
//from Doctrine documentation: findBy(criteria(array), order(array), limit, offset)
$result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC'))
thorinkor
  • 958
  • 1
  • 11
  • 20
2

I simply follow to native documentation doc and look for the annotation that I selected. For example I selected yaml, added configurations to Product.orm.yml and Category.orm.yml files, also add new php attributes to Entity Methods :

protected $products;

public function __construct()
{
    $this->products = new ArrayCollection();
}

for Category.php and

protected $category;

for Product.php

then run php app/console doctrine:generate:entities Acme ,that successfully add new getters and setters

1nstinct
  • 1,745
  • 1
  • 26
  • 30