Skip to content

Commit

Permalink
Fix issue with Model::saveAssociated() and TranslateBehavior
Browse files Browse the repository at this point in the history
When combining saveAssociated() with validate=first and
TranslateBehavior. Saving data for multiple locales was not done
correctly.

Fixes #3272
  • Loading branch information
markstory committed Nov 1, 2012
1 parent 414e0a3 commit 1f31340
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -383,6 +383,21 @@ protected function _setRuntimeData(Model $Model) {
$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
*
Expand Down
32 changes: 32 additions & 0 deletions lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -530,6 +530,38 @@ public function testSaveCreate() {
$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.
*
Expand Down

0 comments on commit 1f31340

Please sign in to comment.