ODM.php 4.4 KB

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