Skip to content
Browse files

BUG Fix DataObject::loadLazyFields discarding original query parameters

BUG Fix Versioned::writeToStage failing to write object with unchanged fields
  • Loading branch information...
1 parent ccb7919 commit 23f5f08eda4201e0d3d4c28b81805da10b55bdb1 @tractorcow tractorcow committed Mar 4, 2014
Showing with 47 additions and 0 deletions.
  1. +5 −0 model/DataObject.php
  2. +1 −0 model/Versioned.php
  3. +41 −0 tests/model/VersionedTest.php
View
5 model/DataObject.php
@@ -2166,6 +2166,11 @@ protected function loadLazyFields($tableClass = null) {
}
$dataQuery = new DataQuery($tableClass);
+
+ // Reset query parameter context to that of this DataObject
+ if($params = $this->getSourceQueryParams()) {
+ foreach($params as $key => $value) $dataQuery->setQueryParam($key, $value);
+ }
// TableField sets the record ID to "new" on new row data, so don't try doing anything in that case
if(!is_numeric($this->record['ID'])) return false;
View
1 model/Versioned.php
@@ -1184,6 +1184,7 @@ public function writeToStage($stage, $forceInsert = false) {
$oldMode = Versioned::get_reading_mode();
Versioned::reading_stage($stage);
+ $this->owner->forceChange();
$result = $this->owner->write(false, $forceInsert);
Versioned::set_reading_mode($oldMode);
View
41 tests/model/VersionedTest.php
@@ -492,6 +492,47 @@ public function testVersionedWithSingleStage() {
'Writes to and reads from default stage even if a non-matching stage is set'
);
}
+
+ /**
+ * Test that publishing processes respects lazy loaded fields
+ */
+ public function testLazyLoadFields() {
+ $originalMode = Versioned::get_reading_mode();
+
+ // Generate staging record and retrieve it from stage in live mode
+ Versioned::reading_stage('Stage');
+ $obj = new VersionedTest_Subclass();
+ $obj->Name = 'bob';
+ $obj->ExtraField = 'Field Value';
+ $obj->write();
+ $objID = $obj->ID;
+ $filter = sprintf('"VersionedTest_DataObject"."ID" = \'%d\'', Convert::raw2sql($objID));
+ Versioned::reading_stage('Live');
+
+ // Check fields are unloaded prior to access
+ $objLazy = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', $filter, false);
+ $lazyFields = $objLazy->getQueriedDatabaseFields();
+ $this->assertTrue(isset($lazyFields['ExtraField_Lazy']));
+ $this->assertEquals('VersionedTest_Subclass', $lazyFields['ExtraField_Lazy']);
+
+ // Check lazy loading works when viewing a Stage object in Live mode
+ $this->assertEquals('Field Value', $objLazy->ExtraField);
+
+ // Test that writeToStage respects lazy loaded fields
+ $objLazy = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', $filter, false);
+ $objLazy->writeToStage('Live');
+ $objLive = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Live', $filter, false);
+ $liveLazyFields = $objLive->getQueriedDatabaseFields();
+
+ // Check fields are unloaded prior to access
+ $this->assertTrue(isset($liveLazyFields['ExtraField_Lazy']));
+ $this->assertEquals('VersionedTest_Subclass', $liveLazyFields['ExtraField_Lazy']);
+
+ // Check that live record has original value
+ $this->assertEquals('Field Value', $objLive->ExtraField);
+
+ Versioned::set_reading_mode($originalMode);
+ }
}

0 comments on commit 23f5f08

Please sign in to comment.
Something went wrong with that request. Please try again.