Skip to content

Commit 1f31340

Browse files
committed
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
1 parent 414e0a3 commit 1f31340

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/Cake/Model/Behavior/TranslateBehavior.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,21 @@ protected function _setRuntimeData(Model $Model) {
383383
$this->runtime[$Model->alias]['beforeSave'] = $tempData;
384384
}
385385

386+
/**
387+
* Restores model data to the original data.
388+
* This solves issues with saveAssociated and validate = first.
389+
*
390+
* @param Model $model
391+
* @return void
392+
*/
393+
public function afterValidate(Model $Model) {
394+
$Model->data[$Model->alias] = array_merge(
395+
$Model->data[$Model->alias],
396+
$this->runtime[$Model->alias]['beforeSave']
397+
);
398+
return true;
399+
}
400+
386401
/**
387402
* afterSave Callback
388403
*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,38 @@ public function testSaveCreate() {
530530
$this->assertEquals($expected, $result);
531531
}
532532

533+
/**
534+
* testSaveAssociatedCreate method
535+
*
536+
* @return void
537+
*/
538+
public function testSaveAssociatedMultipleLocale() {
539+
$this->loadFixtures('Translate', 'TranslatedItem');
540+
541+
$TestModel = new TranslatedItem();
542+
$data = array(
543+
'slug' => 'fourth_translated',
544+
'title' => array(
545+
'eng' => 'Title #4',
546+
'spa' => 'Leyenda #4',
547+
),
548+
'content' => array(
549+
'eng' => 'Content #4',
550+
'spa' => 'Contenido #4',
551+
),
552+
'translated_article_id' => 1,
553+
);
554+
$TestModel->create();
555+
$TestModel->saveAssociated($data);
556+
557+
$translations = array('title' => 'Title', 'content' => 'Content');
558+
$TestModel->bindTranslation($translations, false);
559+
$TestModel->locale = array('eng', 'spa');
560+
$result = $TestModel->read();
561+
$this->assertCount(2, $result['Title']);
562+
$this->assertCount(2, $result['Content']);
563+
}
564+
533565
/**
534566
* Test that saving only some of the translated fields allows the record to be found again.
535567
*

0 commit comments

Comments
 (0)