Php 如何获取实体属性的类型
实际上,我有一个条令实体,我需要动态地填充它的属性 我希望能够做到这样:Php 如何获取实体属性的类型,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,实际上,我有一个条令实体,我需要动态地填充它的属性 我希望能够做到这样: $entity = new Entity(); $reflect = new ReflectionClass($entity); // $fields is an array whihch contain the entity name as the array key and the value as the array value foreach ($fields as $key => $val) { i
$entity = new Entity();
$reflect = new ReflectionClass($entity);
// $fields is an array whihch contain the entity name as the array key and the value as the array value
foreach ($fields as $key => $val)
{
if (!reflect->hasProperty($key)) {
var_dump('the entity does not have a such property');
continue;
}
if ( the type of $key is string ) {
// convert $value to utf8
} elseif ( the type of $key is integer) {
// do something else
} //....etc
}
我该怎么做?您可以使用
找到了。我希望这会有帮助
use Doctrine\Common\Annotations\AnnotationReader;
$docReader = new AnnotationReader();
$entity = new Entity();
$reflect = new ReflectionClass($entity);
// $fields is an array whihch contain the entity name as the array key and the value as the array value
foreach ($fields as $key => $val)
{
if (!reflect->hasProperty($key)) {
var_dump('the entity does not have a such property');
continue;
}
$docInfos = $docReader->getPropertyAnnotations($reflect->getProperty($key));
if ( $docInfos[0]->type === 'string' ) {
// convert $value to utf8
} elseif ( $docInfos[0]->type === 'integer' ) {
// do something else
} //....etc
}
尽管这篇文章很古老,但当我们需要解决布尔字段在某些上下文中总是设置为true的问题时,它确实很有用——谢谢smarber和yoshi。我们的解决方案检测布尔字段(在本例中为PATCH),并使用相应的setter来传播值。(当然,如果没有必要,那就太好了。)
参考:如果您对实体字段的注释很少,则以任何顺序显示。
注意:此代码不用于检测相关实体(多通、一通等)
你可以简单地使用。它可能会提供您需要的有关实体的所有信息。与getProperty($key)->getDocComment完全相同,不会提供更好的信息。我想唯一的方法是获取属性注释/注释并解析它(gotcha:),不过我花了一段时间。Thx这将不起作用,因为我的实体属性没有设置为初始值所以很有用!这两个答案都有效,但我喜欢这个,因为它很简单。你的意思是键是字段名而不是实体名吗?
use Doctrine\Common\Annotations\AnnotationReader;
$docReader = new AnnotationReader();
$entity = new Entity();
$reflect = new ReflectionClass($entity);
// $fields is an array whihch contain the entity name as the array key and the value as the array value
foreach ($fields as $key => $val)
{
if (!reflect->hasProperty($key)) {
var_dump('the entity does not have a such property');
continue;
}
$docInfos = $docReader->getPropertyAnnotations($reflect->getProperty($key));
if ( $docInfos[0]->type === 'string' ) {
// convert $value to utf8
} elseif ( $docInfos[0]->type === 'integer' ) {
// do something else
} //....etc
}
/*
* @PATCH("/entity/{name}")
*/
public function patchEntityAction(Request $request, $entity)
{
...
$form->handleRequest($request);
$manager = $this->getDoctrine()->getManager();
// handle booleans
$metadata = $manager->getClassMetadata($bundle_entity);
$entity_fields = $metadata->getFieldNames();
foreach ($entity_fields as $field) {
$type = $metadata->getTypeOfField($field);
if ($request->request->has("$field") && $type == 'boolean') {
$setter = 'set' . ucfirst($field);
$entity->$setter(!!$request->request->get("$field"));
}
}
$manager->persist($entity);
$manager->flush();
...
}
use Doctrine\Common\Annotations\AnnotationReader;
/**
* @param $entity
* @param $fieldName
* @return mixed|null
*/
private function getFieldType($entity, $fieldName)
{
$annotationReader = new AnnotationReader();
$refClass = new ReflectionClass($entity);
$annotations = $annotationReader->getPropertyAnnotations($refClass->getProperty($fieldName));
if (count($annotations) > 0) {
foreach ($annotations as $annotation) {
if (
$annotation instanceof \Doctrine\ORM\Mapping\Column
&& property_exists($annotation, 'type')
) {
return $annotation->type;
}
}
}
return null;
}