ODM.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Event\Adapter;
  3. use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
  4. use Doctrine\ODM\MongoDB\DocumentManager;
  5. use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
  6. use Doctrine\ODM\MongoDB\Cursor;
  7. /**
  8. * Doctrine event adapter for ODM adapted
  9. * for Translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo\Translatable\Mapping\Event\Adapter
  13. * @subpackage ODM
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. final class ODM extends BaseAdapterODM
  18. {
  19. /**
  20. * Get default LogEntry class used to store the logs
  21. *
  22. * @return string
  23. */
  24. public function getDefaultTranslationClass()
  25. {
  26. return 'Gedmo\\Translatable\\Document\\Translation';
  27. }
  28. /**
  29. * Load the translations for a given object
  30. *
  31. * @param object $object
  32. * @param string $translationClass
  33. * @param string $locale
  34. * @return array
  35. */
  36. public function loadTranslations($object, $translationClass, $locale)
  37. {
  38. $dm = $this->getObjectManager();
  39. $meta = $dm->getClassMetadata(get_class($object));
  40. // load translated content for all translatable fields
  41. $identifier = $this->extractIdentifier($dm, $object);
  42. // construct query
  43. $qb = $dm->createQueryBuilder($translationClass);
  44. $q = $qb->field('foreignKey')->equals($identifier)
  45. ->field('locale')->equals($locale)
  46. ->field('objectClass')->equals($meta->name)
  47. ->getQuery();
  48. $q->setHydrate(false);
  49. $result = $q->execute();
  50. if ($result instanceof Cursor) {
  51. $result = $result->toArray();
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Search for existing translation record
  57. *
  58. * @param mixed $objectId
  59. * @param string $objectClass
  60. * @param string $locale
  61. * @param string $field
  62. * @param string $translationClass
  63. * @return mixed - null if nothing is found, Translation otherwise
  64. */
  65. public function findTranslation($objectId, $objectClass, $locale, $field, $translationClass)
  66. {
  67. $dm = $this->getObjectManager();
  68. $qb = $dm->createQueryBuilder($translationClass);
  69. $q = $qb->field('foreignKey')->equals($objectId)
  70. ->field('locale')->equals($locale)
  71. ->field('field')->equals($field)
  72. ->field('objectClass')->equals($objectClass)
  73. ->getQuery();
  74. $result = $q->execute();
  75. if ($result instanceof Cursor) {
  76. $result = current($result->toArray());
  77. }
  78. $q->setHydrate(false);
  79. return $result;
  80. }
  81. /**
  82. * Removes all associated translations for given object
  83. *
  84. * @param mixed $objectId
  85. * @param string $transClass
  86. * @return void
  87. */
  88. public function removeAssociatedTranslations($objectId, $transClass)
  89. {
  90. $dm = $this->getObjectManager();
  91. $qb = $dm->createQueryBuilder($transClass);
  92. $q = $qb->remove()
  93. ->field('foreignKey')->equals($objectId)
  94. ->getQuery();
  95. return $q->execute();
  96. }
  97. /**
  98. * Inserts the translation record
  99. *
  100. * @param object $translation
  101. * @return void
  102. */
  103. public function insertTranslationRecord($translation)
  104. {
  105. $dm = $this->getObjectManager();
  106. $meta = $dm->getClassMetadata(get_class($translation));
  107. $collection = $dm->getDocumentCollection($meta->name);
  108. $data = array();
  109. foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) {
  110. if (!$meta->isIdentifier($fieldName)) {
  111. $data[$meta->fieldMappings[$fieldName]['name']] = $reflProp->getValue($translation);
  112. }
  113. }
  114. if (!$collection->insert($data)) {
  115. throw new \Gedmo\Exception\RuntimeException('Failed to insert new Translation record');
  116. }
  117. }
  118. }