Browse Source

Fix inconsistent name/alias usage.

TranslateBehavior should always use name instead of alias when
creating/updating/saving new translate records. It already uses name
when finding translations and the mismatch was causing translations to
not be found when saved from an aliased model.

Thanks to Joost de Keijzer for providing the initial patch.

Fixes #3865
mark_story 12 years ago
parent
commit
cbf3228c34

+ 3 - 3
lib/Cake/Model/Behavior/TranslateBehavior.php

@@ -417,7 +417,7 @@ class TranslateBehavior extends ModelBehavior {
 		}
 
 		unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
-		$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
+		$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
 		$RuntimeModel = $this->translateModel($Model);
 
 		if ($created) {
@@ -502,7 +502,7 @@ class TranslateBehavior extends ModelBehavior {
  */
 	public function afterDelete(Model $Model) {
 		$RuntimeModel = $this->translateModel($Model);
-		$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
+		$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
 		$RuntimeModel->deleteAll($conditions);
 	}
 
@@ -611,7 +611,7 @@ class TranslateBehavior extends ModelBehavior {
 					}
 				}
 				$associations[$association] = array_merge($default, array('conditions' => array(
-					'model' => $Model->alias,
+					'model' => $Model->name,
 					$RuntimeModel->displayField => $field
 				)));
 			}

+ 30 - 0
lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php

@@ -532,6 +532,36 @@ class TranslateBehaviorTest extends CakeTestCase {
 	}
 
 /**
+ * test saving/deleting with an alias, uses the model name.
+ *
+ * @return void
+ */
+	public function testSaveDeleteIgnoreAlias() {
+		$this->loadFixtures('Translate', 'TranslatedItem');
+
+		$TestModel = new TranslatedItem(array('alias' => 'SomethingElse'));
+		$TestModel->locale = 'spa';
+		$data = array(
+			'slug' => 'fourth_translated',
+			'title' => 'Leyenda #4',
+			'content' => 'Contenido #4',
+			'translated_article_id' => 1,
+		);
+		$TestModel->create($data);
+		$TestModel->save();
+		$id = $TestModel->id;
+		$result = $TestModel->read();
+		$expected = array($TestModel->alias => array_merge($data, array('id' => $id, 'locale' => 'spa')));
+		$this->assertEquals($expected, $result);
+
+		$TestModel->delete($id);
+		$result = $TestModel->translateModel()->find('count', array(
+			'conditions' => array('foreign_key' => $id)
+		));
+		$this->assertEquals(0, $result);
+	}
+
+/**
  * test save multiple locales method
  *
  * @return void