From cbf3228c346d7bcce5a4bdc1b2293fbc652bd2ed Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 2 Jun 2013 13:48:17 -0400 Subject: [PATCH] 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 --- lib/Cake/Model/Behavior/TranslateBehavior.php | 6 ++-- .../Model/Behavior/TranslateBehaviorTest.php | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index 4bdfcb1e1..eff7b4060 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/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 ))); } diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index f94dcf661..8e202b252 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -531,6 +531,36 @@ class TranslateBehaviorTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * 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 *