Skip to content

Commit 0291c85

Browse files
authored
Fixed accessing to a non-existing key
1 parent ca28496 commit 0291c85

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ a release.
4242
- Wrong PHPDoc type declarations.
4343
- Avoid calling deprecated `AbstractClassMetadataFactory::getCacheDriver()` method.
4444
- Avoid deprecations using `doctrine/mongodb-odm` >= 2.2
45+
- Translatable: `Gedmo\Translatable\Document\Repository\TranslationRepository::findObjectByTranslatedField()`
46+
method accessing a non-existing key.
4547

4648
### Deprecated
4749
- Tree: When using Closure tree strategy, it is deprecated not defining the mapping associations of the closure entity.

src/Translatable/Document/Repository/TranslationRepository.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,32 @@ public function findTranslations($document)
167167
*/
168168
public function findObjectByTranslatedField($field, $value, $class)
169169
{
170-
$document = null;
171170
$meta = $this->dm->getClassMetadata($class);
172-
if ($meta->hasField($field)) {
173-
$qb = $this->createQueryBuilder();
174-
$q = $qb->field('field')->equals($field)
175-
->field('objectClass')->equals($meta->rootDocumentName)
176-
->field('content')->equals($value)
177-
->getQuery();
178171

179-
$q->setHydrate(false);
180-
$result = $q->execute();
181-
if ($result instanceof Iterator) {
182-
$result = $result->toArray();
183-
}
184-
$id = count($result) ? $result[0]['foreignKey'] : null;
185-
if ($id) {
186-
$document = $this->dm->find($class, $id);
187-
}
172+
if (!$meta->hasField($field)) {
173+
return null;
174+
}
175+
176+
$qb = $this->createQueryBuilder();
177+
$q = $qb->field('field')->equals($field)
178+
->field('objectClass')->equals($meta->rootDocumentName)
179+
->field('content')->equals($value)
180+
->getQuery();
181+
182+
$q->setHydrate(false);
183+
$result = $q->execute();
184+
185+
if ($result instanceof Iterator) {
186+
$result = $result->toArray();
187+
}
188+
189+
$id = $result[0]['foreign_key'] ?? null;
190+
191+
if (null === $id) {
192+
return null;
188193
}
189194

190-
return $document;
195+
return $this->dm->find($class, $id);
191196
}
192197

193198
/**

tests/Gedmo/Translatable/TranslatableDocumentTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ public function testTranslation(): void
113113
static::assertCount(0, $translations);
114114
}
115115

116+
public function testFindObjectByTranslatedField(): void
117+
{
118+
$repo = $this->dm->getRepository(self::ARTICLE);
119+
$article = $repo->findOneBy(['title' => 'Title EN']);
120+
static::assertInstanceOf(self::ARTICLE, $article);
121+
122+
$this->translatableListener->setTranslatableLocale('de_de');
123+
$article->setTitle('Title DE');
124+
$article->setCode('Code DE');
125+
126+
$this->dm->persist($article);
127+
$this->dm->flush();
128+
$this->dm->clear();
129+
130+
$transRepo = $this->dm->getRepository(self::TRANSLATION);
131+
static::assertInstanceOf(TranslationRepository::class, $transRepo);
132+
133+
$articleFound = $transRepo->findObjectByTranslatedField(
134+
'title',
135+
'Title DE',
136+
self::ARTICLE
137+
);
138+
static::assertInstanceOf(self::ARTICLE, $articleFound);
139+
140+
static::assertSame($article->getId(), $articleFound->getId());
141+
}
142+
116143
private function populate(): void
117144
{
118145
$art0 = new Article();

0 commit comments

Comments
 (0)