TranslationRepository.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace DoctrineExtensions\Translatable\Repository;
  3. use Doctrine\ORM\EntityRepository,
  4. Doctrine\ORM\Query,
  5. DoctrineExtensions\Translatable\Exception,
  6. DoctrineExtensions\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 DoctrineExtensions.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 instanceof Translatable) {
  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. $qb = $this->_em->createQueryBuilder();
  38. $qb->select('trans.content, trans.field, trans.locale')
  39. ->from($this->_entityName, 'trans')
  40. ->where('trans.foreignKey = :entityId', 'trans.entity = :entityClass')
  41. ->orderBy('trans.locale');
  42. $q = $qb->getQuery();
  43. $data = $q->execute(
  44. compact('entityId', 'entityClass'),
  45. Query::HYDRATE_ARRAY
  46. );
  47. if ($data && is_array($data) && count($data)) {
  48. foreach ($data as $row) {
  49. $result[$row['locale']][$row['field']] = $row['content'];
  50. }
  51. }
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Find the entity $class by the translated field.
  57. * Result is the first occurence of translated field
  58. *
  59. * @param string $field
  60. * @param string $value
  61. * @param string $class
  62. * @return object - instance of $class or null if not found
  63. */
  64. public function findEntityByTranslatedField($field, $value, $class)
  65. {
  66. $entity = null;
  67. $meta = $this->_em->getClassMetadata($class);
  68. if ($meta->hasField($field)) {
  69. $dql = "SELECT trans.foreignKey FROM {$this->_entityName} trans";
  70. $dql .= ' WHERE trans.entity = :class';
  71. $dql .= ' AND trans.field = :field';
  72. $dql .= ' AND trans.content = :value';
  73. $q = $this->_em->createQuery($dql);
  74. $q->setParameters(compact('class', 'field', 'value'));
  75. $q->setMaxResults(1);
  76. $result = $q->getArrayResult();
  77. $id = count($result) ? $result[0]['foreignKey'] : null;
  78. if ($id) {
  79. $entity = $this->_em->find($class, $id);
  80. }
  81. }
  82. return $entity;
  83. }
  84. /**
  85. * Loads all translations with all translatable
  86. * fields by a given entity primary key
  87. *
  88. * @param mixed $id - primary key value of an entity
  89. * @return array
  90. */
  91. public function findTranslationsByEntityId($id)
  92. {
  93. $result = array();
  94. if ($id) {
  95. $qb = $this->_em->createQueryBuilder();
  96. $qb->select('trans.content, trans.field, trans.locale')
  97. ->from($this->_entityName, 'trans')
  98. ->where('trans.foreignKey = :entityId')
  99. ->orderBy('trans.locale');
  100. $q = $qb->getQuery();
  101. $data = $q->execute(
  102. array('entityId' => $id),
  103. Query::HYDRATE_ARRAY
  104. );
  105. if ($data && is_array($data) && count($data)) {
  106. foreach ($data as $row) {
  107. $result[$row['locale']][$row['field']] = $row['content'];
  108. }
  109. }
  110. }
  111. return $result;
  112. }
  113. }