diff --git a/lib/Doctrine/ODM/PHPCR/Translation/TranslationStrategy/AttributeTranslationStrategy.php b/lib/Doctrine/ODM/PHPCR/Translation/TranslationStrategy/AttributeTranslationStrategy.php index 14393781b..1cd04a071 100644 --- a/lib/Doctrine/ODM/PHPCR/Translation/TranslationStrategy/AttributeTranslationStrategy.php +++ b/lib/Doctrine/ODM/PHPCR/Translation/TranslationStrategy/AttributeTranslationStrategy.php @@ -62,7 +62,7 @@ public function saveTranslation(array $data, NodeInterface $node, ClassMetadata $node->setProperty($propName, $propValue); if (null === $propValue) { - $nullFields[] = $field; + $nullFields[] = $mapping['property']; } } if (empty($nullFields)) { @@ -88,7 +88,8 @@ private function checkHasFields(NodeInterface $node, ClassMetadata $metadata, $l } foreach ($metadata->translatableFields as $field) { - $propName = $this->getTranslatedPropertyName($locale, $field); + $mapping = $metadata->mappings[$field]; + $propName = $this->getTranslatedPropertyName($locale, $mapping['property']); if ($node->hasProperty($propName)) { return true; } @@ -110,10 +111,10 @@ public function loadTranslation($document, NodeInterface $node, ClassMetadata $m // we have a translation, now update the document fields foreach ($metadata->translatableFields as $field) { - $propName = $this->getTranslatedPropertyName($locale, $field); + $mapping = $metadata->mappings[$field]; + $propName = $this->getTranslatedPropertyName($locale, $mapping['property']); if ($node->hasProperty($propName)) { $value = $node->getPropertyValue($propName); - $mapping = $metadata->mappings[$field]; if (true === $mapping['multivalue']) { if (isset($mapping['assoc'])) { $transMapping = $this->getTranslatedPropertyNameAssoc($locale, $mapping); @@ -139,7 +140,8 @@ public function loadTranslation($document, NodeInterface $node, ClassMetadata $m public function removeTranslation($document, NodeInterface $node, ClassMetadata $metadata, $locale) { foreach ($metadata->translatableFields as $field) { - $propName = $this->getTranslatedPropertyName($locale, $field); + $mapping = $metadata->mappings[$field]; + $propName = $this->getTranslatedPropertyName($locale, $mapping['property']); if ($node->hasProperty($propName)) { $prop = $node->getProperty($propName); diff --git a/tests/Doctrine/Tests/Models/Translation/Article.php b/tests/Doctrine/Tests/Models/Translation/Article.php index b51306ca1..986eccee3 100644 --- a/tests/Doctrine/Tests/Models/Translation/Article.php +++ b/tests/Doctrine/Tests/Models/Translation/Article.php @@ -65,6 +65,11 @@ class Article */ protected $settings; + /** + * @PHPCRODM\String(assoc="", property="custom-settings", translated=true, nullable=true) + */ + public $customNameSettings; + public function __construct() { $this->children = new ArrayCollection(); diff --git a/tests/Doctrine/Tests/ODM/PHPCR/Functional/Translation/AttributeTranslationStrategyTest.php b/tests/Doctrine/Tests/ODM/PHPCR/Functional/Translation/AttributeTranslationStrategyTest.php index b86f423db..a273cc61a 100644 --- a/tests/Doctrine/Tests/ODM/PHPCR/Functional/Translation/AttributeTranslationStrategyTest.php +++ b/tests/Doctrine/Tests/ODM/PHPCR/Functional/Translation/AttributeTranslationStrategyTest.php @@ -91,6 +91,7 @@ public function testLoadTranslation() $node = $this->getTestNode(); $node->setProperty(self::propertyNameForLocale('en', 'topic'), 'English topic'); $node->setProperty(self::propertyNameForLocale('en', 'text'), 'English text'); + $node->setProperty(self::propertyNameForLocale('en', 'custom-property-name'), 'Custom property name'); $node->setProperty(self::propertyNameForLocale('fr', 'topic'), 'Sujet français'); $node->setProperty(self::propertyNameForLocale('fr', 'text'), 'Texte français'); $node->setProperty('author', 'John Doe'); @@ -106,6 +107,7 @@ public function testLoadTranslation() // And check the translatable properties have the correct value $this->assertEquals('English topic', $doc->topic); $this->assertEquals('English text', $doc->getText()); + $this->assertEquals('Custom property name', $doc->customPropertyName); $this->assertEquals(array(), $doc->getSettings()); // nullable // Load another language and test the document has been updated @@ -143,6 +145,7 @@ public function testTranslationNullProperties() $data['topic'] = 'Some interesting subject'; $data['text'] = 'Lorem ipsum...'; $data['nullable'] = 'not null'; + $data['customPropertyName'] = 'Custom property name'; $data['settings'] = array('key' => 'value'); $node = $this->getTestNode(); @@ -155,6 +158,7 @@ public function testTranslationNullProperties() $data = array(); $data['topic'] = 'Un sujet intéressant'; $data['text'] = 'Lorem français'; + $data['customPropertyName'] = null; $strategy->saveTranslation($data, $node, $this->metadata, 'fr'); $this->dm->flush(); @@ -167,13 +171,20 @@ public function testTranslationNullProperties() $this->assertEquals('Some interesting subject', $doc->topic); $this->assertEquals('Lorem ipsum...', $doc->getText()); $this->assertEquals('not null', $doc->nullable); + $this->assertEquals('Custom property name', $doc->customPropertyName); $this->assertEquals(array('key' => 'value'), $doc->getSettings()); $strategy->loadTranslation($doc, $node, $this->metadata, 'fr'); $this->assertEquals('Un sujet intéressant', $doc->topic); $this->assertEquals('Lorem français', $doc->getText()); $this->assertNull($doc->nullable); + $this->assertNull($doc->customPropertyName); $this->assertEquals(array(), $doc->getSettings()); + + $nullFields = $node->getProperty('phpcr_locale:fr' . AttributeTranslationStrategy::NULLFIELDS)->getValue(); + $this->assertEquals(array( + 'custom-property-name', + ), $nullFields); } public function testRemoveTranslation() @@ -276,6 +287,10 @@ public function testTranslationArrayProperties() 'is-active' => 'true', 'url' => 'great-article-in-english.html' ); + $data['customNameSettings'] = array( + 'is-active' => 'true', + 'url' => 'great-article-in-english.html' + ); $node = $this->getTestNode(); $node->setProperty('author', 'John Doe'); @@ -290,6 +305,10 @@ public function testTranslationArrayProperties() 'is-active' => 'true', 'url' => 'super-article-en-francais.html' ); + $data['customNameSettings'] = array( + 'is-active' => 'true', + 'url' => 'super-article-en-francais.html' + ); $strategy->saveTranslation($data, $node, $this->metadata, 'fr'); $this->dm->flush(); @@ -305,6 +324,10 @@ public function testTranslationArrayProperties() 'is-active' => 'true', 'url' => 'great-article-in-english.html' ), $doc->getSettings()); + $this->assertEquals(array( + 'is-active' => 'true', + 'url' => 'great-article-in-english.html' + ), $doc->customNameSettings); $strategy->loadTranslation($doc, $node, $this->metadata, 'fr'); $this->assertEquals(array('is-active', 'url'), array_keys($doc->getSettings())); @@ -312,6 +335,10 @@ public function testTranslationArrayProperties() 'is-active' => 'true', 'url' => 'super-article-en-francais.html' ), $doc->getSettings()); + $this->assertEquals(array( + 'is-active' => 'true', + 'url' => 'super-article-en-francais.html' + ), $doc->customNameSettings); } public function testQueryBuilder()