ORM.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Gedmo\Translatable\Mapping\Event\Adapter;
  3. use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  6. use Doctrine\ORM\Query;
  7. /**
  8. * Doctrine event adapter for ORM adapted
  9. * for Translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo\Translatable\Mapping\Event\Adapter
  13. * @subpackage ORM
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. final class ORM extends BaseAdapterORM
  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\\Entity\\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. $em = $this->getObjectManager();
  39. $meta = $em->getClassMetadata(get_class($object));
  40. // load translated content for all translatable fields
  41. $objectId = $this->extractIdentifier($em, $object);
  42. // construct query
  43. $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
  44. $dql .= ' WHERE t.foreignKey = :objectId';
  45. $dql .= ' AND t.locale = :locale';
  46. $dql .= ' AND t.objectClass = :objectClass';
  47. // fetch results
  48. $objectClass = $meta->name;
  49. $q = $em->createQuery($dql);
  50. $q->setParameters(compact('objectId', 'locale', 'objectClass'));
  51. return $q->getArrayResult();
  52. }
  53. /**
  54. * Search for existing translation record
  55. *
  56. * @param mixed $objectId
  57. * @param string $objectClass
  58. * @param string $locale
  59. * @param string $field
  60. * @param string $translationClass
  61. * @return mixed - null if nothing is found, Translation otherwise
  62. */
  63. public function findTranslation($objectId, $objectClass, $locale, $field, $translationClass)
  64. {
  65. $em = $this->getObjectManager();
  66. $qb = $em->createQueryBuilder();
  67. $qb->select('trans')
  68. ->from($translationClass, 'trans')
  69. ->where(
  70. 'trans.foreignKey = :objectId',
  71. 'trans.locale = :locale',
  72. 'trans.field = :field',
  73. 'trans.objectClass = :objectClass'
  74. );
  75. $q = $qb->getQuery();
  76. $result = $q->execute(
  77. compact('field', 'locale', 'objectId', 'objectClass'),
  78. Query::HYDRATE_OBJECT
  79. );
  80. if ($result) {
  81. return array_shift($result);
  82. }
  83. return null;
  84. }
  85. /**
  86. * Removes all associated translations for given object
  87. *
  88. * @param mixed $objectId
  89. * @param string $transClass
  90. * @return void
  91. */
  92. public function removeAssociatedTranslations($objectId, $transClass)
  93. {
  94. $em = $this->getObjectManager();
  95. $dql = 'DELETE ' . $transClass . ' trans';
  96. $dql .= ' WHERE trans.foreignKey = :objectId';
  97. $q = $em->createQuery($dql);
  98. $q->setParameters(compact('objectId'));
  99. return $q->getSingleScalarResult();
  100. }
  101. /**
  102. * Inserts the translation record
  103. *
  104. * @param object $translation
  105. * @return void
  106. */
  107. public function insertTranslationRecord($translation)
  108. {
  109. $em = $this->getObjectManager();
  110. $meta = $em->getClassMetadata(get_class($translation));
  111. $data = array();
  112. foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) {
  113. if (!$meta->isIdentifier($fieldName)) {
  114. $data[$meta->getColumnName($fieldName)] = $reflProp->getValue($translation);
  115. }
  116. }
  117. $table = $meta->getTableName();
  118. if (!$em->getConnection()->insert($table, $data)) {
  119. throw new \Gedmo\Exception\RuntimeException('Failed to insert new Translation record');
  120. }
  121. }
  122. }