From b5f918765e296245b31cca2270112c4c1311c64e Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 30 Dec 2011 20:39:04 -0500 Subject: [PATCH] Adding docs + exceptions for name translation. Closes #2367 --- lib/Cake/Model/Behavior/TranslateBehavior.php | 10 +++++++++- .../Case/Model/Behavior/TranslateBehaviorTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index 165ed8065..5ddebb57c 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/lib/Cake/Model/Behavior/TranslateBehavior.php @@ -368,7 +368,10 @@ class TranslateBehavior extends ModelBehavior { /** * Bind translation for fields, optionally with hasMany association for - * fake field + * fake field. + * + * *Note* You should avoid binding translations that overlap existing model properties. + * This can cause un-expected and un-desirable behavior. * * @param Model $model instance of model * @param string|array $fields string with field or array(field1, field2=>AssocName, field3) @@ -391,6 +394,11 @@ class TranslateBehavior extends ModelBehavior { $field = $key; $association = $value; } + if ($field === 'name') { + throw new CakeException( + __d('cake_dev', 'You cannot bind a translation named "name".') + ); + } if (array_key_exists($field, $this->settings[$model->alias])) { unset($this->settings[$model->alias][$field]); diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 8c342a5e4..89fa6a864 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -868,4 +868,16 @@ class TranslateBehaviorTest extends CakeTestCase { $this->assertFalse($result); } + +/** + * Test that an exception is raised when you try to over-write the name attribute. + * + * @expectedException CakeException + * @return void + */ + public function testExceptionOnNameTranslation() { + $this->loadFixtures('Translate', 'TranslatedItem'); + $TestModel = new TranslatedItem(); + $TestModel->bindTranslation(array('name' => 'name')); + } }