I'm trying to use a custom repository for my symfony 2.8 website:
Custom repository:
// src/AppBundle/Entity/ExperimentationRepository
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ExperimentationRepository extends EntityRepository
{
public function getExperimentationByUser($id){
// do stuff and return results
}
}
Entity:
// src/AppBundle/Entity/Experimentation
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Experimentation
*
* @ORM\Table(name="experimentation")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ExperimentationRepository")
*/
class Experimentation{
// sutff
}
Controller (where I try to use the custom repository, in another bundle):
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$experimentations = $em->getRepository('AppBundle:Experimentation')->getExperimentationByUser($id);
I got the following error:
Undefined method 'getExperimentationByUser'. The method name must start with either findBy or findOneBy!
After some research, I've tried to check if my custom repository was called or not:
$repository=$this->getDoctrine()
->getRepository('AppBundle:Experimentation');
$repositoryClass=get_class($repository);
echo $repositoryClass;
exit;
Which returns:
Doctrine\ORM\EntityRepository
So I guess my custom repository isn't called at all. But I can't manage to find why. I've tried several actions (after some digging):
- clear the cache (
app/console cache:clear
) - clear doctrine cache (
app/console doctrine:cache:clear-metadata
) - check config.yml (under doctrine:orm):
auto_mapping: true
- check config.yml (under doctrine:dbal):
type: annotation
- I don't have any config/doctrine folder (is that normal?)
Nothing worked. Still got the same error. It's look like the framework completely ignore the annotation in the entity class. Any idea/suggestion?
Thanks for your help!
app/console doctrine:mapping:info
picks all of them with the "OK" tag. – DavidLORM\Entity(repositoryClass="Test")
. You should get a fatal error:Fatal error: Class 'Test' not found in...
If you don't get this error then the annotation is not read correctly. – Wilt