ODM.php 3.3 KB

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