实际上我有一个教义实体,我需要动态地填充它。
我希望能够做这样的事情:
$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
}
我该怎么做?
多亏了@Yoshi找到了解决方案。我希望它会有所帮助
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 来传播该值。(当然,如果这不是必需的,那就太好了。
/*
* @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();
...
}
参考: https://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Request.html
您可以使用 gettype
$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 ( gettype($key) === "integer" ) {
// convert $value to utf8
} elseif ( gettype($key) === "string") {
// do something else
} //....etc
}
如果您以
任何顺序对实体字段几乎没有不同的注释。
注意:此代码不检测相关实体(多对一,一对多等)。
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;
}