Skip to content
This repository
Browse code

Fix additional issues with saveAll().

Fixes #2857
  • Loading branch information...
commit fbba3621b5e522d1c7f7fe4c175a3298871469de 1 parent a1e06b9
Mark Story authored May 07, 2012
24  lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -318,10 +318,19 @@ public function beforeValidate(Model $model) {
318 318
 /**
319 319
  * beforeSave callback.
320 320
  *
  321
+ * Copies data into the runtime property when `$options['validate']` is
  322
+ * disabled.  Or the runtime data hasn't been set yet.
  323
+ *
321 324
  * @param Model $model Model save was called on.
322 325
  * @return boolean true.
323 326
  */
324  
-	public function beforeSave(Model $model) {
  327
+	public function beforeSave(Model $model, $options = array()) {
  328
+		if (isset($options['validate']) && $options['validate'] == false) {
  329
+			unset($this->runtime[$model->alias]['beforeSave']);
  330
+		}
  331
+		if (isset($this->runtime[$model->alias]['beforeSave'])) {
  332
+			return true;
  333
+		}
325 334
 		$this->_setRuntimeData($model);
326 335
 		return true;
327 336
 	}
@@ -338,7 +347,7 @@ public function beforeSave(Model $model) {
338 347
  */
339 348
 	protected function _setRuntimeData(Model $model) {
340 349
 		$locale = $this->_getLocale($model);
341  
-		if (empty($locale) || isset($this->runtime[$model->alias]['beforeSave'])) {
  350
+		if (empty($locale)) {
342 351
 			return true;
343 352
 		}
344 353
 		$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
@@ -370,12 +379,17 @@ protected function _setRuntimeData(Model $model) {
370 379
  * @return void
371 380
  */
372 381
 	public function afterSave(Model $model, $created) {
373  
-		if (!isset($this->runtime[$model->alias]['beforeSave'])) {
  382
+		if (!isset($this->runtime[$model->alias]['beforeValidate']) && !isset($this->runtime[$model->alias]['beforeSave'])) {
374 383
 			return true;
375 384
 		}
376 385
 		$locale = $this->_getLocale($model);
377  
-		$tempData = $this->runtime[$model->alias]['beforeSave'];
378  
-		unset($this->runtime[$model->alias]['beforeSave']);
  386
+		if (isset($this->runtime[$model->alias]['beforeValidate'])) {
  387
+			$tempData = $this->runtime[$model->alias]['beforeValidate'];
  388
+		} else {
  389
+			$tempData = $this->runtime[$model->alias]['beforeSave'];
  390
+		}
  391
+
  392
+		unset($this->runtime[$model->alias]['beforeValidate'], $this->runtime[$model->alias]['beforeSave']);
379 393
 		$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
380 394
 		$RuntimeModel = $this->translateModel($model);
381 395
 
2  lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -742,6 +742,8 @@ public function testSaveAllTranslatedAssociations() {
742 742
 			'conditions' => array('translated_article_id' => $Model->id)
743 743
 		));
744 744
 		$this->assertCount(2, $result);
  745
+		$this->assertEquals($data['TranslatedItem'][0]['title'], $result[0]['TranslatedItem']['title']);
  746
+		$this->assertEquals($data['TranslatedItem'][1]['title'], $result[1]['TranslatedItem']['title']);
745 747
 	}
746 748
 
747 749
 /**

0 notes on commit fbba362

Please sign in to comment.
Something went wrong with that request. Please try again.