Skip to content

Commit

Permalink
Merge pull request doctrine#603 from dantleech/translated_props
Browse files Browse the repository at this point in the history
Read translated custom property name
  • Loading branch information
dbu committed Mar 6, 2015
2 parents 4e470ba + 31a5c39 commit cf7b1e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Models/Translation/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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()
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand All @@ -305,13 +324,21 @@ 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()));
$this->assertEquals(array(
'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()
Expand Down

0 comments on commit cf7b1e3

Please sign in to comment.