ModelManager.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  15. use Doctrine\ORM\EntityManager;
  16. class ModelManager implements ModelManagerInterface
  17. {
  18. protected $entityManager;
  19. /**
  20. *
  21. * @param \Doctrine\ORM\EntityManager $entityManager
  22. */
  23. public function __construct(EntityManager $entityManager)
  24. {
  25. $this->entityManager = $entityManager;
  26. }
  27. /**
  28. * Returns the related model's metadata
  29. *
  30. * @abstract
  31. * @param string $name
  32. * @return void
  33. */
  34. public function getMetadata($class)
  35. {
  36. return $this->entityManager->getMetadataFactory()->getMetadataFor($class);
  37. }
  38. /**
  39. * Returns true is the model has some metadata
  40. *
  41. * @return boolean
  42. */
  43. public function hasMetadata($class)
  44. {
  45. return $this->entityManager->getMetadataFactory()->hasMetadataFor($class);
  46. }
  47. /**
  48. * Returns a new FieldDescription
  49. *
  50. * @abstract
  51. * @return \Sonata\AdminBundle\Admin\ORM\FieldDescription
  52. */
  53. public function getNewFieldDescriptionInstance($class, $name, array $options = array())
  54. {
  55. $metadata = $this->getMetadata($class);
  56. $fieldDescription = new FieldDescription;
  57. $fieldDescription->setName($name);
  58. $fieldDescription->setOptions($options);
  59. if (isset($metadata->associationMappings[$name])) {
  60. $fieldDescription->setAssociationMapping($metadata->associationMappings[$name]);
  61. }
  62. if (isset($metadata->fieldMappings[$name])) {
  63. $fieldDescription->setFieldMapping($metadata->fieldMappings[$name]);
  64. }
  65. return $fieldDescription;
  66. }
  67. public function create($object)
  68. {
  69. $this->entityManager->persist($object);
  70. $this->entityManager->flush();
  71. }
  72. public function update($object)
  73. {
  74. $this->entityManager->persist($object);
  75. $this->entityManager->flush();
  76. }
  77. public function delete($object)
  78. {
  79. $this->entityManager->remove($object);
  80. $this->entityManager->flush();
  81. }
  82. public function find($class, $id)
  83. {
  84. return $this->entityManager->find($class, $id);
  85. }
  86. /**
  87. * @return \Doctrine\ORM\EntityManager
  88. */
  89. public function getEntityManager()
  90. {
  91. return $this->entityManager;
  92. }
  93. /**
  94. * @param string $parentAssociationMapping
  95. * @param string $class
  96. * @return \Sonata\AdminBundle\Admin\ORM\FieldDescription
  97. */
  98. public function getParentFieldDescription($parentAssociationMapping, $class)
  99. {
  100. $fieldName = $parentAssociationMapping['fieldName'];
  101. $metadata = $this->getMetadata($class);
  102. $associatingMapping = $metadata->associationMappings[$parentAssociationMapping];
  103. $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName);
  104. $fieldDescription->setName($parentAssociationMapping);
  105. $fieldDescription->setAssociationMapping($associatingMapping);
  106. return $fieldDescription;
  107. }
  108. /**
  109. * @param string $alias
  110. * @return \Doctrine\ORM\QueryBuilder a query instance
  111. */
  112. public function createQuery($class, $alias = 'o')
  113. {
  114. $repository = $this->getEntityManager()->getRepository($class);
  115. return $repository->createQueryBuilder($alias);
  116. }
  117. /**
  118. * @param string $class
  119. * @return string
  120. */
  121. public function getEntityIdentifier($class)
  122. {
  123. return $this->getMetadata($class)->identifier;
  124. }
  125. /**
  126. * Deletes a set of $class identified by the provided $idx array
  127. *
  128. * @param string $class
  129. * @param array $idx
  130. * @return void
  131. */
  132. public function batchDelete($class, $idx)
  133. {
  134. $queryBuilder = $this->createQuery($class, 'o');
  135. $objects = $queryBuilder
  136. ->select('o')
  137. ->add('where', $queryBuilder->expr()->in('o.id', $idx))
  138. ->getQuery()
  139. ->execute();
  140. foreach ($objects as $object) {
  141. $this->entityManager->remove($object);
  142. }
  143. $this->entityManager->flush();
  144. }
  145. /**
  146. * Returns a new model instance
  147. * @param string $class
  148. * @return
  149. */
  150. public function getModelInstance($class)
  151. {
  152. return new $class;
  153. }
  154. /**
  155. * Returns the parameters used in the columns header
  156. *
  157. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  158. * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
  159. * @return string
  160. */
  161. public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
  162. {
  163. $values = $datagrid->getValues();
  164. if ($fieldDescription->getOption('sortable') == $values['_sort_by']) {
  165. if ($values['_sort_order'] == 'ASC') {
  166. $values['_sort_order'] = 'DESC';
  167. } else {
  168. $values['_sort_order'] = 'ASC';
  169. }
  170. } else {
  171. $values['_sort_order'] = 'ASC';
  172. $values['_sort_by'] = $fieldDescription->getOption('sortable');
  173. }
  174. return $values;
  175. }
  176. /**
  177. * @param sring $class
  178. * @return array
  179. */
  180. public function getDefaultSortValues($class)
  181. {
  182. return array(
  183. '_sort_order' => 'ASC',
  184. '_sort_by' => implode(',', $this->getEntityIdentifier($class))
  185. );
  186. }
  187. }