600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > PHP中如何判断属性类型 php – 如何获取doctrine实体属性的类型

PHP中如何判断属性类型 php – 如何获取doctrine实体属性的类型

时间:2022-11-09 18:16:44

相关推荐

PHP中如何判断属性类型 php  – 如何获取doctrine实体属性的类型

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

我希望能够做到这样的事情:

$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

}

我该怎么做呢?

解决方法:

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

}

标签:php,symfony,doctrine-orm

来源: https://codeday.me/bug/0716/1476739.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。