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
This commit is contained in:
mark_story 2012-10-31 23:13:56 -04:00
parent 414e0a3484
commit 1f31340a2a
2 changed files with 47 additions and 0 deletions

View file

@ -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
*

View file

@ -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.
*