Home » Php » php – custom repository class in symfony2

php – custom repository class in symfony2

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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 ….

Answer:

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

Answer:

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

Answer:

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

Answer:

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