mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
414e0a3484
commit
1f31340a2a
2 changed files with 47 additions and 0 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue