如何获取学说实体属性的类型 [英] how to get the type of a doctrine entity property

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

问题描述

实际上我有一个学说实体,我需要动态填充其属性.

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
}

我该怎么做?

推荐答案

Found 感谢@Yoshi 的解决方案.希望能帮到你

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

use DoctrineCommonAnnotationsAnnotationReader;

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆