Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 1f31340a2a3f200a7bd8a9329ab22c38a7005397 1 parent 414e0a3
@markstory markstory authored
View
15 lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -384,6 +384,21 @@ protected function _setRuntimeData(Model $Model) {
}
/**
+ * 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
*
* @param Model $Model Model the callback is called on
View
32 lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -531,6 +531,38 @@ public function testSaveCreate() {
}
/**
+ * 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.
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.