Browse files

Adding translations to the whitelist of associations to be saved

  • Loading branch information...
1 parent 1d1bb62 commit 892a217dd9ea5c4a624d1c9d7a913ec0152a35b5 @lorenzo lorenzo committed Feb 10, 2014
View
5 src/Model/Behavior/TranslateBehavior.php
@@ -148,20 +148,23 @@ public function beforeFind(Event $event, $query) {
}, $query::PREPEND);
}
- public function beforeSave(Event $event, $entity) {
+ public function beforeSave(Event $event, $entity, $options) {
$locale = $entity->get('_locale') ?: $this->locale();
if (!$locale) {
return;
}
+ $newOptions = ['I18n' => ['validate' => false]];
+ $options['associated'] = $newOptions + $options['associated'];
$values = $entity->extract($this->config()['fields'], true);
$fields = array_keys($values);
$key = $entity->get(current((array)$this->_table->primaryKey()));
$preexistent = TableRegistry::get('I18n')->find()
->select(['id', 'field'])
->where(['field IN' => $fields, 'locale' => $locale, 'foreign_key' => $key])
+ ->bufferResults(false)
->indexBy('field');
$modified = [];
View
21 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
@@ -548,4 +548,25 @@ public function testUpdateTranslationWithLocaleInEntity() {
$this->assertEquals('First Article Body', $article->get('body'));
}
+/**
+ * Tests that translations are added to the whitelist of associations to be
+ * saved
+ *
+ * @return void
+ */
+ public function testSaveTranslationWithAssociationWhitelist() {
+ $table = TableRegistry::get('Articles');
+ $table->hasMany('Comments');
+ $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+ $table->locale('fra');
+
+ $article = $table->find()->first();
+ $this->assertEquals(1, $article->get('id'));
+ $article->set('title', 'Le titre');
+ $table->save($article, ['associated' => ['Comments']]);
+
+ $article = $table->find()->first();
+ $this->assertEquals('Le titre', $article->get('title'));
+ }
+
}

0 comments on commit 892a217

Please sign in to comment.