Skip to content

Commit cf7b1e3

Browse files
committed
Merge pull request #603 from dantleech/translated_props
Read translated custom property name
2 parents 4e470ba + 31a5c39 commit cf7b1e3

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

lib/Doctrine/ODM/PHPCR/Translation/TranslationStrategy/AttributeTranslationStrategy.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function saveTranslation(array $data, NodeInterface $node, ClassMetadata
6262
$node->setProperty($propName, $propValue);
6363

6464
if (null === $propValue) {
65-
$nullFields[] = $field;
65+
$nullFields[] = $mapping['property'];
6666
}
6767
}
6868
if (empty($nullFields)) {
@@ -88,7 +88,8 @@ private function checkHasFields(NodeInterface $node, ClassMetadata $metadata, $l
8888
}
8989

9090
foreach ($metadata->translatableFields as $field) {
91-
$propName = $this->getTranslatedPropertyName($locale, $field);
91+
$mapping = $metadata->mappings[$field];
92+
$propName = $this->getTranslatedPropertyName($locale, $mapping['property']);
9293
if ($node->hasProperty($propName)) {
9394
return true;
9495
}
@@ -110,10 +111,10 @@ public function loadTranslation($document, NodeInterface $node, ClassMetadata $m
110111

111112
// we have a translation, now update the document fields
112113
foreach ($metadata->translatableFields as $field) {
113-
$propName = $this->getTranslatedPropertyName($locale, $field);
114+
$mapping = $metadata->mappings[$field];
115+
$propName = $this->getTranslatedPropertyName($locale, $mapping['property']);
114116
if ($node->hasProperty($propName)) {
115117
$value = $node->getPropertyValue($propName);
116-
$mapping = $metadata->mappings[$field];
117118
if (true === $mapping['multivalue']) {
118119
if (isset($mapping['assoc'])) {
119120
$transMapping = $this->getTranslatedPropertyNameAssoc($locale, $mapping);
@@ -139,7 +140,8 @@ public function loadTranslation($document, NodeInterface $node, ClassMetadata $m
139140
public function removeTranslation($document, NodeInterface $node, ClassMetadata $metadata, $locale)
140141
{
141142
foreach ($metadata->translatableFields as $field) {
142-
$propName = $this->getTranslatedPropertyName($locale, $field);
143+
$mapping = $metadata->mappings[$field];
144+
$propName = $this->getTranslatedPropertyName($locale, $mapping['property']);
143145

144146
if ($node->hasProperty($propName)) {
145147
$prop = $node->getProperty($propName);

tests/Doctrine/Tests/Models/Translation/Article.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class Article
6565
*/
6666
protected $settings;
6767

68+
/**
69+
* @PHPCRODM\String(assoc="", property="custom-settings", translated=true, nullable=true)
70+
*/
71+
public $customNameSettings;
72+
6873
public function __construct()
6974
{
7075
$this->children = new ArrayCollection();

tests/Doctrine/Tests/ODM/PHPCR/Functional/Translation/AttributeTranslationStrategyTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function testLoadTranslation()
9191
$node = $this->getTestNode();
9292
$node->setProperty(self::propertyNameForLocale('en', 'topic'), 'English topic');
9393
$node->setProperty(self::propertyNameForLocale('en', 'text'), 'English text');
94+
$node->setProperty(self::propertyNameForLocale('en', 'custom-property-name'), 'Custom property name');
9495
$node->setProperty(self::propertyNameForLocale('fr', 'topic'), 'Sujet français');
9596
$node->setProperty(self::propertyNameForLocale('fr', 'text'), 'Texte français');
9697
$node->setProperty('author', 'John Doe');
@@ -106,6 +107,7 @@ public function testLoadTranslation()
106107
// And check the translatable properties have the correct value
107108
$this->assertEquals('English topic', $doc->topic);
108109
$this->assertEquals('English text', $doc->getText());
110+
$this->assertEquals('Custom property name', $doc->customPropertyName);
109111
$this->assertEquals(array(), $doc->getSettings()); // nullable
110112

111113
// Load another language and test the document has been updated
@@ -143,6 +145,7 @@ public function testTranslationNullProperties()
143145
$data['topic'] = 'Some interesting subject';
144146
$data['text'] = 'Lorem ipsum...';
145147
$data['nullable'] = 'not null';
148+
$data['customPropertyName'] = 'Custom property name';
146149
$data['settings'] = array('key' => 'value');
147150

148151
$node = $this->getTestNode();
@@ -155,6 +158,7 @@ public function testTranslationNullProperties()
155158
$data = array();
156159
$data['topic'] = 'Un sujet intéressant';
157160
$data['text'] = 'Lorem français';
161+
$data['customPropertyName'] = null;
158162

159163
$strategy->saveTranslation($data, $node, $this->metadata, 'fr');
160164
$this->dm->flush();
@@ -167,13 +171,20 @@ public function testTranslationNullProperties()
167171
$this->assertEquals('Some interesting subject', $doc->topic);
168172
$this->assertEquals('Lorem ipsum...', $doc->getText());
169173
$this->assertEquals('not null', $doc->nullable);
174+
$this->assertEquals('Custom property name', $doc->customPropertyName);
170175
$this->assertEquals(array('key' => 'value'), $doc->getSettings());
171176

172177
$strategy->loadTranslation($doc, $node, $this->metadata, 'fr');
173178
$this->assertEquals('Un sujet intéressant', $doc->topic);
174179
$this->assertEquals('Lorem français', $doc->getText());
175180
$this->assertNull($doc->nullable);
181+
$this->assertNull($doc->customPropertyName);
176182
$this->assertEquals(array(), $doc->getSettings());
183+
184+
$nullFields = $node->getProperty('phpcr_locale:fr' . AttributeTranslationStrategy::NULLFIELDS)->getValue();
185+
$this->assertEquals(array(
186+
'custom-property-name',
187+
), $nullFields);
177188
}
178189

179190
public function testRemoveTranslation()
@@ -276,6 +287,10 @@ public function testTranslationArrayProperties()
276287
'is-active' => 'true',
277288
'url' => 'great-article-in-english.html'
278289
);
290+
$data['customNameSettings'] = array(
291+
'is-active' => 'true',
292+
'url' => 'great-article-in-english.html'
293+
);
279294

280295
$node = $this->getTestNode();
281296
$node->setProperty('author', 'John Doe');
@@ -290,6 +305,10 @@ public function testTranslationArrayProperties()
290305
'is-active' => 'true',
291306
'url' => 'super-article-en-francais.html'
292307
);
308+
$data['customNameSettings'] = array(
309+
'is-active' => 'true',
310+
'url' => 'super-article-en-francais.html'
311+
);
293312

294313
$strategy->saveTranslation($data, $node, $this->metadata, 'fr');
295314
$this->dm->flush();
@@ -305,13 +324,21 @@ public function testTranslationArrayProperties()
305324
'is-active' => 'true',
306325
'url' => 'great-article-in-english.html'
307326
), $doc->getSettings());
327+
$this->assertEquals(array(
328+
'is-active' => 'true',
329+
'url' => 'great-article-in-english.html'
330+
), $doc->customNameSettings);
308331

309332
$strategy->loadTranslation($doc, $node, $this->metadata, 'fr');
310333
$this->assertEquals(array('is-active', 'url'), array_keys($doc->getSettings()));
311334
$this->assertEquals(array(
312335
'is-active' => 'true',
313336
'url' => 'super-article-en-francais.html'
314337
), $doc->getSettings());
338+
$this->assertEquals(array(
339+
'is-active' => 'true',
340+
'url' => 'super-article-en-francais.html'
341+
), $doc->customNameSettings);
315342
}
316343

317344
public function testQueryBuilder()

0 commit comments

Comments
 (0)