Home > Software design >  How to getRepository() for entity in subfolder with symfony 5.4?
How to getRepository() for entity in subfolder with symfony 5.4?

Time:08-29

Prior to Symfony 5.4 I used to reference my entities like this

public function findAction($archived = false)
{
    $em = $this->getDoctrine()->getManager();
    $analyses = $em->getRepository('App:Hazardlog\Analysis')->findAll();

I remember there was a new syntax for it ending with ::class a while back (possibly already in symfony 4) but I've been hanging on to this syntax because I have my entities organized into subfolders (a remnant from the old days when code in src was supposed to reside in a bunch of bundles...).

However, now all I get is a neat error saying

Class 'Analysis::class' does not exist

This is my directory structure

Entity
  Hazardlog
    Analysis
    Risk

Now, I have tried a number of ways to reference my entities with the ::class at the end, but none seem to work.

Can I simply not keep my directories in symfony 5.4 or is there a way of doing it that I just didn't find?

I've tried

$analyses = $em->getRepository('App:Hazardlog\Analysis::class')->findAll();

Also tried

$analyses = $em->getRepository('Hazardlog\Analysis::class')->findAll();

As well as

$analyses = $em->getRepository('Analysis::class')->findAll();

(last one was a longshot I know, but since there is only one Analysis entity in the app, and symfony is set on a new syntax, I thought maybe symfony would be smart enough to find it anyways

  • Related