ODM.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, $targetClass)
  74. {
  75. $dm = $this->getObjectManager();
  76. $qb = $dm->createQueryBuilder($transClass);
  77. $q = $qb->remove()
  78. ->field('foreignKey')->equals($objectId)
  79. ->field('objectClass')->equals($targetClass)
  80. ->getQuery()
  81. ;
  82. return $q->execute();
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function insertTranslationRecord($translation)
  88. {
  89. $dm = $this->getObjectManager();
  90. $meta = $dm->getClassMetadata(get_class($translation));
  91. $collection = $dm->getDocumentCollection($meta->name);
  92. $data = array();
  93. foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) {
  94. if (!$meta->isIdentifier($fieldName)) {
  95. $data[$meta->fieldMappings[$fieldName]['name']] = $reflProp->getValue($translation);
  96. }
  97. }
  98. if (!$collection->insert($data)) {
  99. throw new \Gedmo\Exception\RuntimeException('Failed to insert new Translation record');
  100. }
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function getTranslationValue($object, $field, $value = false)
  106. {
  107. $dm = $this->getObjectManager();
  108. $wrapped = AbstractWrapper::wrapp($object, $dm);
  109. $meta = $wrapped->getMetadata();
  110. $mapping = $meta->getFieldMapping($field);
  111. $type = Type::getType($mapping['type']);
  112. if ($value === false) {
  113. $value = $wrapped->getPropertyValue($field);
  114. }
  115. return $type->convertToDatabaseValue($value);
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function setTranslationValue($object, $field, $value)
  121. {
  122. $dm = $this->getObjectManager();
  123. $wrapped = AbstractWrapper::wrapp($object, $dm);
  124. $meta = $wrapped->getMetadata();
  125. $mapping = $meta->getFieldMapping($field);
  126. $type = Type::getType($mapping['type']);
  127. $value = $type->convertToPHPValue($value);
  128. $wrapped->setPropertyValue($field, $value);
  129. }
  130. }