本文介绍了如何获得一个学说实体属性的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有一个学说实体,我需要动态地填充它的价值。

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

我想要这样做:

$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。我希望它会帮助

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

使用Doctrine\Common\Annotations\AnnotationReader;

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
}

这篇关于如何获得一个学说实体属性的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:48