Fix associated translations being inserted.

Due to changes introduced in [1c0b6c076a]
associated translations would incorrectly be saved with a value of ''.

Fixes #3057
This commit is contained in:
mark_story 2012-07-22 20:59:24 -04:00
parent 6286b43329
commit e6ef218600
2 changed files with 37 additions and 7 deletions

View file

@ -395,9 +395,12 @@ class TranslateBehavior extends ModelBehavior {
$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] = '';
}
}

View file

@ -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)
*
@ -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)
*/
@ -1058,4 +1053,36 @@ class TranslateBehaviorTest extends CakeTestCase {
$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']);
}
}