Skip to content

Commit e6ef218

Browse files
committed
Fix associated translations being inserted.
Due to changes introduced in [1c0b6c0] associated translations would incorrectly be saved with a value of ''. Fixes #3057
1 parent 6286b43 commit e6ef218

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

lib/Cake/Model/Behavior/TranslateBehavior.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,12 @@ public function afterSave(Model $model, $created) {
395395

396396
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
397397
if ($created) {
398-
foreach ($fields as $field) {
398+
// set each field value to an empty string
399+
foreach ($fields as $key => $field) {
400+
if (!is_numeric($key)) {
401+
$field = $key;
402+
}
399403
if (!isset($tempData[$field])) {
400-
//set the field value to an empty string
401404
$tempData[$field] = '';
402405
}
403406
}

lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22
/**
3-
* TranslateBehaviorTest file
4-
*
5-
* PHP 5
6-
*
73
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
84
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
95
*
@@ -12,7 +8,6 @@
128
*
139
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
1410
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
15-
* @package Cake.Test.Case.Model.Behavior
1611
* @since CakePHP(tm) v 1.2.0.5669
1712
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1813
*/
@@ -1058,4 +1053,36 @@ public function testUnbindTranslation() {
10581053
$this->assertNotContains('slug', $result);
10591054
}
10601055

1056+
/**
1057+
* Test that additional records are not inserted for associated translations.
1058+
*
1059+
* @return void
1060+
*/
1061+
public function testNoExtraRowsForAssociatedTranslations() {
1062+
$this->loadFixtures('Translate', 'TranslatedItem');
1063+
$TestModel = new TranslatedItem();
1064+
$TestModel->locale = 'spa';
1065+
$TestModel->unbindTranslation();
1066+
$TestModel->bindTranslation(array('name' => 'nameTranslate'));
1067+
1068+
$data = array(
1069+
'TranslatedItem' => array(
1070+
'slug' => 'spanish-name',
1071+
'name' => 'Spanish name',
1072+
),
1073+
);
1074+
$TestModel->create($data);
1075+
$TestModel->save();
1076+
1077+
$Translate = $TestModel->translateModel();
1078+
$results = $Translate->find('all', array(
1079+
'conditions' => array(
1080+
'locale' => $TestModel->locale,
1081+
'foreign_key' => $TestModel->id
1082+
)
1083+
));
1084+
$this->assertCount(1, $results, 'Only one field should be saved');
1085+
$this->assertEquals('name', $results[0]['TranslateTestModel']['field']);
1086+
}
1087+
10611088
}

0 commit comments

Comments
 (0)