Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the type of a doctrine entity property

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?

like image 949
smarber Avatar asked Dec 04 '14 11:12

smarber


People also ask

What is doctrine database?

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.

What is Entity Manager in doctrine?

The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.

What is doctrine Symfony?

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.


2 Answers

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
}
like image 148
smarber Avatar answered Sep 22 '22 01:09

smarber


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

like image 41
Vincent Avatar answered Sep 19 '22 01:09

Vincent