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.

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

6 Comments

Reuven I got the correct output,but in my Netbeans ide couldn't detect my custom repository function.y? do you know it?
I don't think it's possible for netbean to know the custom repository. My version of netbean does not autocomplete symfony specific code.
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 ?
me too. i also dont have the doctrine directory.. do i have to create it first and then create the mentioned file ?
this is the syntax for yml entity configuration. For annotation, please refer to the doctrine documentation
|
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 ....

1 Comment

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
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
{
...
}`   

1 Comment

Weird, that also worked for me after spending the last 2 hours trying to work out what the issue was.
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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.