kristyan
kristyan

Reputation: 81

how doctrine entityRepository works?

i'm new on doctrine orm so i try to learn some tutorial on

ps:knpuniversity.com/screencast/doctrine-queries/dql

but i still confused for exp:

src/AppBundle/Entity/Category.php

class Category
{

    private $id;

    private $name;

    private $iconKey;

    private $fortuneCookies;

We can access build in method by

$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');
        $categories = $categoryRepository->findAll();

But if we want to access new method we can do this

$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');
        $categories = $categoryRepository->findAllOrdered();

Then provide class for repository

src/AppBundle/Entity/CategoryRepository.php

class CategoryRepository extends EntityRepository
{
    public function findAllOrdered()
    {
        die('this query will blow your mind...');
    }
}

i still not get it!!

$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');

if those above point to category entity, how come it suddenly can link to CategoryRepository and we could access findAllOrdered() method?

did i missed something guys please help?

Upvotes: 1

Views: 529

Answers (2)

Jesús Castro
Jesús Castro

Reputation: 2119

As several developers told you, it's important to link your Entity class with your Repository class through the annotations.

  • @ORM\Table(name="your_entity_name")
  • @ORM\Entity(repositoryClass="Your_RepositoryClass")

If you want to call your repository with a name more customized (e.g. 'repository.category' instead of 'AppBundle:Category'), you can create a service through a yml file.

Upvotes: 1

Vladislav
Vladislav

Reputation: 357

As I understood you are using annotations and most probably you have an annotation of the entity class. You just have to specify the repository class like this:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\CategoryRepository")
 * @ORM\Table(name="Category")
 */

Specify

Upvotes: 1

Related Questions