Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit cbf3228c346d7bcce5a4bdc1b2293fbc652bd2ed 1 parent a6bc927
@markstory markstory authored
View
6 lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -417,7 +417,7 @@ public function afterSave(Model $Model, $created) {
}
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 @@ protected function _prepareTranslations(Model $Model, $data) {
*/
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 @@ public function bindTranslation(Model $Model, $fields, $reset = true) {
}
}
$associations[$association] = array_merge($default, array('conditions' => array(
- 'model' => $Model->alias,
+ 'model' => $Model->name,
$RuntimeModel->displayField => $field
)));
}
View
30 lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -532,6 +532,36 @@ public function testSaveCreate() {
}
/**
+ * 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
Please sign in to comment.
Something went wrong with that request. Please try again.