Skip to content

Commit

Permalink
Fix associated translations being inserted.
Browse files Browse the repository at this point in the history
Due to changes introduced in [1c0b6c0]
associated translations would incorrectly be saved with a value of ''.

Fixes #3057
  • Loading branch information
markstory committed Jul 23, 2012
1 parent 6286b43 commit e6ef218
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -395,9 +395,12 @@ public function afterSave(Model $model, $created) {

$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
if ($created) {
foreach ($fields as $field) {
// set each field value to an empty string
foreach ($fields as $key => $field) {
if (!is_numeric($key)) {
$field = $key;
}
if (!isset($tempData[$field])) {
//set the field value to an empty string
$tempData[$field] = '';
}
}
Expand Down
37 changes: 32 additions & 5 deletions lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -1,9 +1,5 @@
<?php
/**
* TranslateBehaviorTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -12,7 +8,6 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Model.Behavior
* @since CakePHP(tm) v 1.2.0.5669
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
Expand Down Expand Up @@ -1058,4 +1053,36 @@ public function testUnbindTranslation() {
$this->assertNotContains('slug', $result);
}

/**
* Test that additional records are not inserted for associated translations.
*
* @return void
*/
public function testNoExtraRowsForAssociatedTranslations() {
$this->loadFixtures('Translate', 'TranslatedItem');
$TestModel = new TranslatedItem();
$TestModel->locale = 'spa';
$TestModel->unbindTranslation();
$TestModel->bindTranslation(array('name' => 'nameTranslate'));

$data = array(
'TranslatedItem' => array(
'slug' => 'spanish-name',
'name' => 'Spanish name',
),
);
$TestModel->create($data);
$TestModel->save();

$Translate = $TestModel->translateModel();
$results = $Translate->find('all', array(
'conditions' => array(
'locale' => $TestModel->locale,
'foreign_key' => $TestModel->id
)
));
$this->assertCount(1, $results, 'Only one field should be saved');
$this->assertEquals('name', $results[0]['TranslateTestModel']['field']);
}

}

0 comments on commit e6ef218

Please sign in to comment.