Permalink
Browse files

Fix unbindTranslation not unbinding.

There were documented use cases that have never worked.  Fix that.
Also rename a method so it better describes what it does.

Fixes #2913
  • Loading branch information...
1 parent b27a3aa commit 0bfcd492498708aef074b832f56e8a89b5d48c4f @markstory markstory committed May 28, 2012
@@ -484,7 +484,8 @@ public function translateModel(Model $model) {
*
* @param Model $model instance of model
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
- * @param boolean $reset
+ * @param boolean $reset Leave true to have the fields only modified for the next operation.
+ * if false the field will be added for all future queries.
* @return boolean
* @throws CakeException when attempting to bind a translating called name. This is not allowed
* as it shadows Model::$name.
@@ -511,7 +512,7 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
);
}
- $this->_updateSettings($model, $field);
+ $this->_removeField($model, $field);
if (is_null($association)) {
if ($reset) {
@@ -553,17 +554,17 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
*
* @param string $field The field to update.
*/
- protected function _updateSettings(Model $model, $field) {
+ protected function _removeField(Model $model, $field) {
if (array_key_exists($field, $this->settings[$model->alias])) {
unset($this->settings[$model->alias][$field]);
} elseif (in_array($field, $this->settings[$model->alias])) {
- $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
+ $this->settings[$model->alias] = array_merge(array_diff($this->settings[$model->alias], array($field)));
}
if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
unset($this->runtime[$model->alias]['fields'][$field]);
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
- $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
+ $this->runtime[$model->alias]['fields'] = array_merge(array_diff($this->runtime[$model->alias]['fields'], array($field)));
}
}
@@ -599,7 +600,7 @@ public function unbindTranslation(Model $model, $fields = null) {
$association = $value;
}
- $this->_updateSettings($model, $field);
+ $this->_removeField($model, $field);
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association;
@@ -1015,4 +1015,28 @@ public function testExceptionOnNameTranslation() {
$TestModel = new TranslatedItem();
$TestModel->bindTranslation(array('name' => 'name'));
}
+
+/**
+ * Test that translations can be bound and unbound dynamically.
+ *
+ * @return void
+ */
+ public function testUnbindTranslation() {
+ $this->loadFixtures('Translate', 'TranslatedItem');
+ $Model = new TranslatedItem();
+ $Model->unbindTranslation();
+ $Model->bindTranslation(array('body', 'slug'), false);
+
+ $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
+ $this->assertEquals(array('body', 'slug'), $result);
+
+ $Model->unbindTranslation(array('body'));
+ $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
+ $this->assertNotContains('body', $result);
+
+ $Model->unbindTranslation('slug');
+ $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
+ $this->assertNotContains('slug', $result);
+ }
+
}

0 comments on commit 0bfcd49

Please sign in to comment.