1

Is it a bad practice to have a generic helper class that creates and hydrates entity object? Example:

class EntityHelper {
    public static function createEntity($entityName, array $entityData, EntityManager $em) {
        $entity = new $entityName;
        $entityFields = $em->getClassMetadata($entityName)->getFieldNames();
       foreach ($entityFields as $field) {
            if (in_array($field, array_keys($entityData))) {
                $entity->{'set' . ucfirst($field)}($entityData[$field]);
            }   
        }
        return $entity;
    }
}

If it is a bad practice could anyone please suggest a better way of doing it.

Fabio
  • 23,183
  • 12
  • 55
  • 64
Rohith Mohan
  • 187
  • 1
  • 14

1 Answers1

1

Not really sure what you are trying to do there (and the most important : why), but you can take a look at the IoC pattern

https://en.wikipedia.org/wiki/Inversion_of_control

Understanding IoC Containers and Dependency Injection

Maybe a better way to create object of the correct type when you need it.

Thanks

Community
  • 1
  • 1
Iteration
  • 494
  • 2
  • 7
  • 18