From 1f31340a2a3f200a7bd8a9329ab22c38a7005397 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 31 Oct 2012 23:13:56 -0400 Subject: [PATCH] Fix issue with Model::saveAssociated() and TranslateBehavior When combining saveAssociated() with validate=first and TranslateBehavior. Saving data for multiple locales was not done correctly. Fixes #3272 --- lib/Cake/Model/Behavior/TranslateBehavior.php | 15 +++++++++ .../Model/Behavior/TranslateBehaviorTest.php | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index dd1243b91..01a2c0d53 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/lib/Cake/Model/Behavior/TranslateBehavior.php @@ -383,6 +383,21 @@ class TranslateBehavior extends ModelBehavior { $this->runtime[$Model->alias]['beforeSave'] = $tempData; } +/** + * Restores model data to the original data. + * This solves issues with saveAssociated and validate = first. + * + * @param Model $model + * @return void + */ + public function afterValidate(Model $Model) { + $Model->data[$Model->alias] = array_merge( + $Model->data[$Model->alias], + $this->runtime[$Model->alias]['beforeSave'] + ); + return true; + } + /** * afterSave Callback * diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index a73ccd17c..50d897d60 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -530,6 +530,38 @@ class TranslateBehaviorTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * testSaveAssociatedCreate method + * + * @return void + */ + public function testSaveAssociatedMultipleLocale() { + $this->loadFixtures('Translate', 'TranslatedItem'); + + $TestModel = new TranslatedItem(); + $data = array( + 'slug' => 'fourth_translated', + 'title' => array( + 'eng' => 'Title #4', + 'spa' => 'Leyenda #4', + ), + 'content' => array( + 'eng' => 'Content #4', + 'spa' => 'Contenido #4', + ), + 'translated_article_id' => 1, + ); + $TestModel->create(); + $TestModel->saveAssociated($data); + + $translations = array('title' => 'Title', 'content' => 'Content'); + $TestModel->bindTranslation($translations, false); + $TestModel->locale = array('eng', 'spa'); + $result = $TestModel->read(); + $this->assertCount(2, $result['Title']); + $this->assertCount(2, $result['Content']); + } + /** * Test that saving only some of the translated fields allows the record to be found again. *