您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractFactory.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Lc\SovBundle\Factory;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. abstract class AbstractFactory
  5. {
  6. protected $em ;
  7. public function __construct(EntityManagerInterface $em)
  8. {
  9. $this->em = $em ;
  10. }
  11. public function create($params = array())
  12. {
  13. $entityClass = $this->em->getEntityName($this->getEntityClass());
  14. if ($entityClass && strlen($entityClass) > 0) {
  15. $entity = new $entityClass();
  16. $this->populate($entity, $params);
  17. return $entity;
  18. }
  19. else {
  20. throw new \ErrorException('La classe de l\'entité n\'est pas définie.');
  21. }
  22. }
  23. public function populate($entity, $params = array())
  24. {
  25. $entityArray = (array)$entity;
  26. foreach ($entityArray as $name => $value) {
  27. $nameArray = explode("\x00", $name);
  28. $name = $nameArray[2];
  29. if ($name != 'id' && isset($params[$name])) {
  30. $setter = 'set' . ucfirst($name);
  31. if (method_exists($entity, $setter)) {
  32. $entity->$setter($params[$name]);
  33. }
  34. }
  35. }
  36. }
  37. }