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
This commit is contained in:
mark_story 2014-02-11 21:56:05 -05:00
parent a5d50da040
commit 96a37d5917
2 changed files with 34 additions and 2 deletions

View file

@ -279,7 +279,13 @@ class TranslateBehavior extends ModelBehavior {
*/
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 @@ class TranslateBehavior extends ModelBehavior {
}
$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 @@ class TranslateBehavior extends ModelBehavior {
__d('cake_dev', 'You cannot bind a translation named "name".')
);
}
$this->_removeField($Model, $field);
if ($association === null) {
@ -604,6 +612,7 @@ class TranslateBehavior extends ModelBehavior {
} else {
if ($reset) {
$this->runtime[$Model->alias]['fields'][$field] = $association;
$this->runtime[$Model->alias]['restoreFields'][] = $field;
} else {
$this->settings[$Model->alias][$field] = $association;
}

View file

@ -1057,6 +1057,29 @@ class TranslateBehaviorTest extends CakeTestCase {
$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
*