Custom Repository Class in Symfony2

custom repository class in symfony2

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

Custom Repository Classes in Symfony2

You don't need the "getNumberOfTasks" method at all.

What you should be doing is specifying the associations in your Doctrine Entities.

You can read about it here:

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

Based on your naming, I would suspect a OneToMany relationship from Project to Task, and a ManyToOne relationship from Task to Project.

When mapped correctly, in Twig you can then use:

{{ project.tasks|length }} 

to get the number of tasks in a project. All you need to do is to pass the Project Entity to Twig and Symfony will handle the rest, including loading in all relationships.

This is the best way of doing what you are trying to do.

Custom repository class in symfony 2 not working

Thanks for everyone who was helping. I figured it out, IT WAS MYSELF of course there was nothing wrong with symfony but much more with what I did:

After the command:

 php app/console generate:bundle --namespace=Acme/StoreBundle

I choose for

"Determine the format to use for the generated configuration." --> xml

And not annotation. So I had to change in my

~/Projects/Symfony/src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.xml

file. the <entity name="Acme\StoreBundle\Entity\Product"> to

<entity
name="Acme\StoreBundle\Entity\Product"
repository-class="Acme\StoreBundle\Entity\ProductRepository">

How to use a custom repository in Symfony?

The problem was that I declared my function as private instead of public

private function findByTitle($term){

instead of

public function findByTitle($term){


Related Topics



Leave a reply



Submit