3
votes

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!

3
Have you generated entities?AnkiiG
Quite a mystery. You only have one entity manager defined? I suspect a simple typo somewhere. Does app/console doctrine:mapping:info pick up the entity? And yes it is normal to have config/doctrine to be empty when using annotations. config/doctrine is where yaml or xml mapping files normally live. In fact if you had files there then the annotations would be ignored.Cerad
Entities are generated. app/console doctrine:mapping:info picks all of them with the "OK" tag.DavidL
Try once to change your entity annotation to ORM\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
Grasping for straws here. Are you positive you have edited the correct Experimentation.php file? I have sometimes gotten confused and edited a completely different file in a different project or have forgotten to save my changes. Have you checked your code into github? I'll take a look if you have. Got to be a simple config error somewhere.Cerad

3 Answers

0
votes

Did you try like that:

// src/ManageBundle/Controller/ManageController

$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:Experimentation');
$experimentations = $repository->getExperimentationByUser($id);

And generate entity

php app/console doctrine:generate:entities Experimentation
0
votes

I finally manage to make it work. After several commands, I've managed to create the AppBundle/Resources/config/doctrine file. It was full of .orm.xml files, so I just took the XML syntax from the doc:

  <entity
     name="AppBundle\Entity\Experimentation"
     repository-class="AppBundle\Entity\ExperimentationRepository">

However, I still don't understand why the annotation system is not working, since I've tried so hard to enable it. Is there at least a way to check what system (XML, YAML or Annotation) is enabled? Feel free to comment if you have a clue...

0
votes

You just delete cache folder. clear cache with CLI command didn't work for me. php app/console cache:clear