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?
The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.
The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.
Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.
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
}
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.)
/*
* @PATCH("/entity/{name}")
*/
public function patchEntityAction(Request $request, $entity)
{
...
$form->handleRequest($request);
$manager = $this->getDoctrine()->getManager();
// handle booleans
$metadata = $manager->getClassMetadata($bundle_entity);
$entity_fields = $metadata->getFieldNames();
foreach ($entity_fields as $field) {
$type = $metadata->getTypeOfField($field);
if ($request->request->has("$field") && $type == 'boolean') {
$setter = 'set' . ucfirst($field);
$entity->$setter(!!$request->request->get("$field"));
}
}
$manager->persist($entity);
$manager->flush();
...
}
Ref: https://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Request.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With