ODM.php 4.3 KB

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