2

I looked at the similar questions but couldn't find a solution to my problem.

In my project symfony, I get the information from my database with this code

$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');

But only, the LocalInformation entity, its region attribute and the value of this attribute 'west', are variable and obtained from an ajax request. So I have

$value = trim($data['value']);
$property = trim($data['property']);
$entity = trim($data['entity']);
 
$class = ucfirst($entity) . '::class';
$findByProperty = 'findBy' . ucfirst($property);
 
dump($class);
dump($property);
dump($value);
 
$backup = $this->getDoctrine()->getRepository($class)->$findByProperty($value);

I am getting the following error: 'Unknown Entity namespace alias 'LocalInformations'.

And when I comment the last line, the dumps give me "LocalInformations::class", "region", "west"

I noticed the first problem, I have

$backup = $this->getDoctrine()->getRepository("LocalInformations::class")->findByRegion('west');

instead of

$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');

"LocalInformations::class" is a string not a class name. Now the problem becomes, how do you remove quotes around a string?

  • have you attempted getRepository(str_replace('"', "", "LocalInformations::class")) or getRepository(str_replace('"', "", $class)).Technically it should return string for computation whereby quotes are replaced. I believe it should do trick if whole bottle neck is quotes it self. under the same premise you could apply the above to code level at $class = ucfirst($entity) . '::class'; as in $class = str_replace('"', "", "ucfirst($entity) . '::class'"); bit of spaghetti but should work. – Syed Dec 12 '20 at 02:37
  • With getRepository(str_replace('"', "", "LocalInformations::class")), getRepository(str_replace('"', "", $class)), $class = ucfirst($entity) . '::class'; and str_replace('"', "", "ucfirst($entity) . '::class'"); I am getting the exact same error. – DONGMO BERNARD GERAUD Dec 12 '20 at 05:52
  • With getRepository(str_replace('"', "", "LocalInformations::class")), getRepository(str_replace('"', "", $class)), $class = ucfirst($entity) . '::class'; and str_replace('"', "", "ucfirst($entity) . '::class'"); I am getting the exact same error : Unknown Entity namespace alias 'LocalInformations'. The problem is that str_replace returns a string. The goal is to get the class name LocalInformations without quotes – DONGMO BERNARD GERAUD Dec 12 '20 at 05:59
  • have read at this https://stackoverflow.com/questions/11067797/unknown-entity-namespace-alias-in-symfony2 – Syed Dec 12 '20 at 21:11

1 Answers1

1

The ::class wouldn't be so helpful since you would need to have the class in your use statements for this to work. The ::class would normally return the FQCN so why not using it directly.

$class = sprintf('\App\Entity\%s', ucfirst($entity));
$findByProperty = 'findBy' . ucfirst($property);
 
$backup = $this->getDoctrine()->getRepository($class)->$findByProperty($value);

Make sure to use the proper namespace for your entities...

Also, make sure to validate that the user should be able to request the entity. Never trust user input as it can be forged easily in most cases.

Julien B.
  • 3,023
  • 2
  • 18
  • 33