TranslationRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Gedmo\Translatable\Repository;
  3. use Doctrine\ORM\EntityRepository,
  4. Doctrine\ORM\Query,
  5. Gedmo\Translatable\Exception,
  6. Gedmo\Translatable\Translatable;
  7. /**
  8. * The TranslationRepository has some useful functions
  9. * to interact with translations.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Translatable.Repository
  13. * @subpackage TranslationRepository
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class TranslationRepository extends EntityRepository
  18. {
  19. /**
  20. * Loads all translations with all translatable
  21. * fields from the given entity
  22. *
  23. * @param object $entity Must implement Translatable
  24. * @return array list of translations in locale groups
  25. */
  26. public function findTranslations($entity)
  27. {
  28. $result = array();
  29. if ($entity) {
  30. if ($this->_em->getUnitOfWork()->getEntityState($entity) == \Doctrine\ORM\UnitOfWork::STATE_NEW) {
  31. return $result;
  32. }
  33. $entityClass = get_class($entity);
  34. $meta = $this->_em->getClassMetadata($entityClass);
  35. $identifier = $meta->getSingleIdentifierFieldName();
  36. $entityId = $meta->getReflectionProperty($identifier)->getValue($entity);
  37. $translationMeta = $this->getClassMetadata(); // table inheritance support
  38. $qb = $this->_em->createQueryBuilder();
  39. $qb->select('trans.content, trans.field, trans.locale')
  40. ->from($translationMeta->rootEntityName, 'trans')
  41. ->where('trans.foreignKey = :entityId', 'trans.entity = :entityClass')
  42. ->orderBy('trans.locale');
  43. $q = $qb->getQuery();
  44. $data = $q->execute(
  45. compact('entityId', 'entityClass'),
  46. Query::HYDRATE_ARRAY
  47. );
  48. if ($data && is_array($data) && count($data)) {
  49. foreach ($data as $row) {
  50. $result[$row['locale']][$row['field']] = $row['content'];
  51. }
  52. }
  53. }
  54. return $result;
  55. }
  56. /**
  57. * Find the entity $class by the translated field.
  58. * Result is the first occurence of translated field.
  59. * Query can be slow, since there are no indexes on such
  60. * columns
  61. *
  62. * @param string $field
  63. * @param string $value
  64. * @param string $class
  65. * @return object - instance of $class or null if not found
  66. */
  67. public function findEntityByTranslatedField($field, $value, $class)
  68. {
  69. $entity = null;
  70. $meta = $this->_em->getClassMetadata($class);
  71. $translationMeta = $this->getClassMetadata(); // table inheritance support
  72. if ($meta->hasField($field)) {
  73. $dql = "SELECT trans.foreignKey FROM {$translationMeta->rootEntityName} trans";
  74. $dql .= ' WHERE trans.entity = :class';
  75. $dql .= ' AND trans.field = :field';
  76. $dql .= ' AND trans.content = :value';
  77. $q = $this->_em->createQuery($dql);
  78. $q->setParameters(compact('class', 'field', 'value'));
  79. $q->setMaxResults(1);
  80. $result = $q->getArrayResult();
  81. $id = count($result) ? $result[0]['foreignKey'] : null;
  82. if ($id) {
  83. $entity = $this->_em->find($class, $id);
  84. }
  85. }
  86. return $entity;
  87. }
  88. /**
  89. * Loads all translations with all translatable
  90. * fields by a given entity primary key
  91. *
  92. * @param mixed $id - primary key value of an entity
  93. * @return array
  94. */
  95. public function findTranslationsByEntityId($id)
  96. {
  97. $result = array();
  98. if ($id) {
  99. $translationMeta = $this->getClassMetadata(); // table inheritance support
  100. $qb = $this->_em->createQueryBuilder();
  101. $qb->select('trans.content, trans.field, trans.locale')
  102. ->from($translationMeta->rootEntityName, 'trans')
  103. ->where('trans.foreignKey = :entityId')
  104. ->orderBy('trans.locale');
  105. $q = $qb->getQuery();
  106. $data = $q->execute(
  107. array('entityId' => $id),
  108. Query::HYDRATE_ARRAY
  109. );
  110. if ($data && is_array($data) && count($data)) {
  111. foreach ($data as $row) {
  112. $result[$row['locale']][$row['field']] = $row['content'];
  113. }
  114. }
  115. }
  116. return $result;
  117. }
  118. }