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
}
How do I do this?
You can use gettype
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.)
Ref: https://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Request.html
Found the solution thanks to @Yoshi. I hope it'll help