TranslationRepository.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * @version 2.0.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class TranslationRepository extends EntityRepository
  19. {
  20. /**
  21. * Loads all translations with all translatable
  22. * fields from the given entity
  23. *
  24. * @param object $entity Must implement Translatable
  25. * @return array list of translations in locale groups
  26. */
  27. public function findTranslations($entity)
  28. {
  29. $result = array();
  30. if ($entity) {
  31. if ($this->_em->getUnitOfWork()->getEntityState($entity) == \Doctrine\ORM\UnitOfWork::STATE_NEW) {
  32. return $result;
  33. }
  34. $entityClass = get_class($entity);
  35. $meta = $this->_em->getClassMetadata($entityClass);
  36. $identifier = $meta->getSingleIdentifierFieldName();
  37. $entityId = $meta->getReflectionProperty($identifier)->getValue($entity);
  38. $qb = $this->_em->createQueryBuilder();
  39. $qb->select('trans.content, trans.field, trans.locale')
  40. ->from($this->_entityName, '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. if ($meta->hasField($field)) {
  72. $dql = "SELECT trans.foreignKey FROM {$this->_entityName} trans";
  73. $dql .= ' WHERE trans.entity = :class';
  74. $dql .= ' AND trans.field = :field';
  75. $dql .= ' AND trans.content = :value';
  76. $q = $this->_em->createQuery($dql);
  77. $q->setParameters(compact('class', 'field', 'value'));
  78. $q->setMaxResults(1);
  79. $result = $q->getArrayResult();
  80. $id = count($result) ? $result[0]['foreignKey'] : null;
  81. if ($id) {
  82. $entity = $this->_em->find($class, $id);
  83. }
  84. }
  85. return $entity;
  86. }
  87. /**
  88. * Loads all translations with all translatable
  89. * fields by a given entity primary key
  90. *
  91. * @param mixed $id - primary key value of an entity
  92. * @return array
  93. */
  94. public function findTranslationsByEntityId($id)
  95. {
  96. $result = array();
  97. if ($id) {
  98. $qb = $this->_em->createQueryBuilder();
  99. $qb->select('trans.content, trans.field, trans.locale')
  100. ->from($this->_entityName, 'trans')
  101. ->where('trans.foreignKey = :entityId')
  102. ->orderBy('trans.locale');
  103. $q = $qb->getQuery();
  104. $data = $q->execute(
  105. array('entityId' => $id),
  106. Query::HYDRATE_ARRAY
  107. );
  108. if ($data && is_array($data) && count($data)) {
  109. foreach ($data as $row) {
  110. $result[$row['locale']][$row['field']] = $row['content'];
  111. }
  112. }
  113. }
  114. return $result;
  115. }
  116. }