ModelManager.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Model\ORM;
  11. use Sonata\AdminBundle\Model\ModelManagerInterface;
  12. use Sonata\AdminBundle\Admin\ORM\FieldDescription;
  13. use Doctrine\ORM\EntityManager;
  14. class ModelManager implements ModelManagerInterface
  15. {
  16. protected $entityManager;
  17. /**
  18. *
  19. * @param \Doctrine\ORM\EntityManager $entityManager
  20. */
  21. public function __construct(EntityManager $entityManager)
  22. {
  23. $this->entityManager = $entityManager;
  24. }
  25. /**
  26. * Returns the related model's metadata
  27. *
  28. * @abstract
  29. * @param string $name
  30. * @return void
  31. */
  32. public function getMetadata($class)
  33. {
  34. return $this->entityManager->getMetadataFactory()->getMetadataFor($class);
  35. }
  36. /**
  37. * Returns true is the model has some metadata
  38. *
  39. * @return boolean
  40. */
  41. public function hasMetadata($class)
  42. {
  43. return $this->entityManager->getMetadataFactory()->hasMetadataFor($class);
  44. }
  45. /**
  46. * Returns a new FieldDescription
  47. *
  48. * @abstract
  49. * @return \Sonata\AdminBundle\Admin\ORM\FieldDescription
  50. */
  51. public function getNewFieldDescriptionInstance($class, $name, array $options = array())
  52. {
  53. $metadata = $this->getMetadata($class);
  54. $fieldDescription = new FieldDescription;
  55. $fieldDescription->setName($name);
  56. $fieldDescription->setOptions($options);
  57. if (isset($metadata->associationMappings[$name])) {
  58. $fieldDescription->setAssociationMapping($metadata->associationMappings[$name]);
  59. }
  60. if (isset($metadata->fieldMappings[$name])) {
  61. $fieldDescription->setFieldMapping($metadata->fieldMappings[$name]);
  62. }
  63. return $fieldDescription;
  64. }
  65. public function create($object)
  66. {
  67. $this->entityManager->persist($object);
  68. $this->entityManager->flush();
  69. }
  70. public function update($object)
  71. {
  72. $this->entityManager->persist($object);
  73. $this->entityManager->flush();
  74. }
  75. public function delete($object)
  76. {
  77. $this->entityManager->remove($object);
  78. $this->entityManager->flush();
  79. }
  80. public function find($class, $id)
  81. {
  82. return $this->entityManager->find($class, $id);
  83. }
  84. /**
  85. * @return \Doctrine\ORM\EntityManager
  86. */
  87. public function getEntityManager()
  88. {
  89. return $this->entityManager;
  90. }
  91. /**
  92. * @param string $parentAssociationMapping
  93. * @param string $class
  94. * @return \Sonata\AdminBundle\Admin\ORM\FieldDescription
  95. */
  96. public function getParentFieldDescription($parentAssociationMapping, $class)
  97. {
  98. $fieldName = $parentAssociationMapping['fieldName'];
  99. $metadata = $this->getMetadata($class);
  100. $associatingMapping = $metadata->associationMappings[$parentAssociationMapping];
  101. $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName);
  102. $fieldDescription->setName($parentAssociationMapping);
  103. $fieldDescription->setAssociationMapping($associatingMapping);
  104. return $fieldDescription;
  105. }
  106. /**
  107. * @param string $alias
  108. * @return \Doctrine\ORM\QueryBuilder a query instance
  109. */
  110. public function createQuery($class, $alias = 'o')
  111. {
  112. $repository = $this->getEntityManager()->getRepository($class);
  113. return $repository->createQueryBuilder($alias);
  114. }
  115. /**
  116. * @param string $class
  117. * @return string
  118. */
  119. public function getEntityIdentifier($class)
  120. {
  121. return $this->getEntityManager()->getUnitOfWork()->getEntityIdentifier($class);
  122. }
  123. /**
  124. * Deletes a set of $class identified by the provided $idx array
  125. *
  126. * @param string $class
  127. * @param array $idx
  128. * @return void
  129. */
  130. public function batchDelete($class, $idx)
  131. {
  132. $queryBuilder = $this->createQuery($class, 'o');
  133. $objects = $queryBuilder
  134. ->select('o')
  135. ->add('where', $queryBuilder->expr()->in('o.id', $idx))
  136. ->getQuery()
  137. ->execute();
  138. foreach ($objects as $object) {
  139. $this->entityManager->remove($object);
  140. }
  141. $this->entityManager->flush();
  142. }
  143. /**
  144. * Returns a new model instance
  145. * @param string $class
  146. * @return
  147. */
  148. public function getModelInstance($class)
  149. {
  150. return new $class;
  151. }
  152. }