how to get the type of a doctrine entity property

后端 未结 4 692
温柔的废话
温柔的废话 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: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
    }
    

提交回复
热议问题