Skip to content

Commit

Permalink
Fix additional issues with saveAll().
Browse files Browse the repository at this point in the history
Fixes #2857
  • Loading branch information
markstory authored and lorenzo committed May 11, 2012
1 parent e53074d commit d9bf3cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -318,10 +318,19 @@ public function beforeValidate(Model $model) {
/**
* beforeSave callback.
*
* Copies data into the runtime property when `$options['validate']` is
* disabled. Or the runtime data hasn't been set yet.
*
* @param Model $model Model save was called on.
* @return boolean true.
*/
public function beforeSave(Model $model) {
public function beforeSave(Model $model, $options = array()) {
if (isset($options['validate']) && $options['validate'] == false) {
unset($this->runtime[$model->alias]['beforeSave']);
}
if (isset($this->runtime[$model->alias]['beforeSave'])) {
return true;
}
$this->_setRuntimeData($model);
return true;
}
Expand All @@ -338,7 +347,7 @@ public function beforeSave(Model $model) {
*/
protected function _setRuntimeData(Model $model) {
$locale = $this->_getLocale($model);
if (empty($locale) || isset($this->runtime[$model->alias]['beforeSave'])) {
if (empty($locale)) {
return true;
}
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
Expand Down Expand Up @@ -370,12 +379,17 @@ protected function _setRuntimeData(Model $model) {
* @return void
*/
public function afterSave(Model $model, $created) {
if (!isset($this->runtime[$model->alias]['beforeSave'])) {
if (!isset($this->runtime[$model->alias]['beforeValidate']) && !isset($this->runtime[$model->alias]['beforeSave'])) {
return true;
}
$locale = $this->_getLocale($model);
$tempData = $this->runtime[$model->alias]['beforeSave'];
unset($this->runtime[$model->alias]['beforeSave']);
if (isset($this->runtime[$model->alias]['beforeValidate'])) {
$tempData = $this->runtime[$model->alias]['beforeValidate'];
} else {
$tempData = $this->runtime[$model->alias]['beforeSave'];
}

unset($this->runtime[$model->alias]['beforeValidate'], $this->runtime[$model->alias]['beforeSave']);
$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
$RuntimeModel = $this->translateModel($model);

Expand Down
2 changes: 2 additions & 0 deletions lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -733,6 +733,8 @@ public function testSaveAllTranslatedAssociations() {
'conditions' => array('translated_article_id' => $Model->id)
));
$this->assertCount(2, $result);
$this->assertEquals($data['TranslatedItem'][0]['title'], $result[0]['TranslatedItem']['title']);
$this->assertEquals($data['TranslatedItem'][1]['title'], $result[1]['TranslatedItem']['title']);
}

/**
Expand Down

0 comments on commit d9bf3cf

Please sign in to comment.