how to get the type of a doctrine entity property

2020-03-04 07:31发布

Actually I have a doctrine entity that I need to fill its prperties dynamically.

I'd like to be able to do something like this:

$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
}

How do I do this?

3条回答
对你真心纯属浪费
2楼-- · 2020-03-04 07:35

You can use 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
}
查看更多
Root(大扎)
3楼-- · 2020-03-04 07:38

Old as it is, this post was really useful when we needed a workaround for boolean fields being always set to true in some contexts -- thank you smarber and yoshi. Our workaround detects boolean fields (in PATCH, in this case) and uses the corresponding setter to propagate the value. (Of course, it would be nice if this weren't necessary.)

/*
 * @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();

    ...

}

Ref: https://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Request.html

查看更多
来,给爷笑一个
4楼-- · 2020-03-04 07:44

Found the solution thanks to @Yoshi. I hope it'll help

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
}
查看更多
登录 后发表回答