未知实体名称空间别名 'entity name'
Unknown Entity namespace alias 'entity name'
我查看了类似的问题,但找不到解决我的问题的方法。
在我的 symfony 项目中,我使用这段代码从我的数据库中获取信息
$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');
但只有 LocalInformation 实体、其区域属性和此属性的值 'west' 是可变的,并且是从 ajax 请求中获得的。所以我有
$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);
我收到以下错误:
'未知实体命名空间别名'LocalInformations'.
当我评论最后一行时,转储给了我
"LocalInformations::class", "地区", "西部"
我注意到第一个问题,我有
$backup = $this->getDoctrine()->getRepository("LocalInformations::class")->findByRegion('west');
而不是
$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');
"LocalInformations::class" 是一个字符串而不是 class 名称。现在问题变成了,如何删除字符串周围的引号?
::class
不会有太大帮助,因为您需要在 use 语句中包含 class 才能工作。 ::class
通常会 return FQCN 所以为什么不直接使用它。
$class = sprintf('\App\Entity\%s', ucfirst($entity));
$findByProperty = 'findBy' . ucfirst($property);
$backup = $this->getDoctrine()->getRepository($class)->$findByProperty($value);
确保为您的实体使用正确的命名空间...
此外,请确保验证用户应该能够请求该实体。永远不要相信用户输入,因为在大多数情况下它很容易被伪造。
我查看了类似的问题,但找不到解决我的问题的方法。
在我的 symfony 项目中,我使用这段代码从我的数据库中获取信息
$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');
但只有 LocalInformation 实体、其区域属性和此属性的值 'west' 是可变的,并且是从 ajax 请求中获得的。所以我有
$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);
我收到以下错误: '未知实体命名空间别名'LocalInformations'.
当我评论最后一行时,转储给了我 "LocalInformations::class", "地区", "西部"
我注意到第一个问题,我有
$backup = $this->getDoctrine()->getRepository("LocalInformations::class")->findByRegion('west');
而不是
$backup = $this->getDoctrine()->getRepository(LocalInformations::class)->findByRegion('west');
"LocalInformations::class" 是一个字符串而不是 class 名称。现在问题变成了,如何删除字符串周围的引号?
::class
不会有太大帮助,因为您需要在 use 语句中包含 class 才能工作。 ::class
通常会 return FQCN 所以为什么不直接使用它。
$class = sprintf('\App\Entity\%s', ucfirst($entity));
$findByProperty = 'findBy' . ucfirst($property);
$backup = $this->getDoctrine()->getRepository($class)->$findByProperty($value);
确保为您的实体使用正确的命名空间...
此外,请确保验证用户应该能够请求该实体。永远不要相信用户输入,因为在大多数情况下它很容易被伪造。