how to get the type of a doctrine entity property

后端 未结 4 693
温柔的废话
温柔的废话 2021-01-06 04:09

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         


        
相关标签:
4条回答
  • 2021-01-06 04:34

    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
    }
    
    0 讨论(0)
  • 2021-01-06 04:42

    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
    }
    
    0 讨论(0)
  • 2021-01-06 04:46

    For case if you have few different annotations for entity field in any order.
    Note: this code is not detect related entities (ManyToOne, OneToMany and 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;
    }
    
    0 讨论(0)
  • 2021-01-06 04:52

    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

    0 讨论(0)
提交回复
热议问题