Permalink
Browse files

Fix temporary associations that replace fields not being restored.

When binding temporary associations, fields should be restored
in the afterFind as the association will have become unbound.

Fixes #2816
  • Loading branch information...
markstory committed Feb 12, 2014
1 parent a5d50da commit 96a37d5917307baa9cf9229cda94da0fc6686fbc
@@ -279,7 +279,13 @@ protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale)
*/
public function afterFind(Model $Model, $results, $primary = false) {
$Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
+
$this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
+ if (!empty($this->runtime[$Model->alias]['restoreFields'])) {
+ $this->runtime[$Model->alias]['fields'] = $this->runtime[$Model->alias]['restoreFields'];
+ unset($this->runtime[$Model->alias]['restoreFields']);
+ }
+
$locale = $this->_getLocale($Model);
if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
@@ -577,7 +583,10 @@ public function bindTranslation(Model $Model, $fields, $reset = true) {
}
$associations = array();
$RuntimeModel = $this->translateModel($Model);
- $default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
+ $default = array(
+ 'className' => $RuntimeModel->alias,
+ 'foreignKey' => 'foreign_key'
+ );
foreach ($fields as $key => $value) {
if (is_numeric($key)) {
@@ -592,7 +601,6 @@ public function bindTranslation(Model $Model, $fields, $reset = true) {
__d('cake_dev', 'You cannot bind a translation named "name".')
);
}
-
$this->_removeField($Model, $field);
if ($association === null) {
@@ -604,6 +612,7 @@ public function bindTranslation(Model $Model, $fields, $reset = true) {
} else {
if ($reset) {
$this->runtime[$Model->alias]['fields'][$field] = $association;
+ $this->runtime[$Model->alias]['restoreFields'][] = $field;
} else {
$this->settings[$Model->alias][$field] = $association;
}
@@ -1057,6 +1057,29 @@ public function testValidation() {
$this->assertFalse(empty($result));
}
+/**
+ * test restoring fields after temporary binds method
+ *
+ * @return void
+ */
+ public function testFieldsRestoreAfterBind() {
+ $this->loadFixtures('Translate', 'TranslatedItem');
+
+ $TestModel = new TranslatedItem();
+
+ $translations = array('title' => 'Title');
+ $TestModel->bindTranslation($translations);
+
+ $result = $TestModel->find('first');
+ $this->assertArrayHasKey('Title', $result);
+ $this->assertArrayHasKey('content', $result['Title'][0]);
+ $this->assertArrayNotHasKey('title', $result);
+
+ $result = $TestModel->find('first');
+ $this->assertArrayNotHasKey('Title', $result);
+ $this->assertEquals('Title #1', $result['TranslatedItem']['title']);
+ }
+
/**
* testAttachDetach method
*

0 comments on commit 96a37d5

Please sign in to comment.