Skip to content

Commit

Permalink
Adding translations to the whitelist of associations to be saved
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 10, 2014
1 parent 1d1bb62 commit 892a217
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -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 = [];
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -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.