Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

FIX 7934 When lazy loading fields respect version of the record

  • Loading branch information...
commit e2bf9649f39f4f966d4085a52fa2da0c9ba5bedf 1 parent b25b6d4
@patbolo patbolo authored chillu committed
View
9 model/DataObject.php
@@ -2075,7 +2075,11 @@ protected function loadLazyFields($tableClass = null) {
// 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;
- $dataQuery->where("\"$tableClass\".\"ID\" = {$this->record['ID']}")->limit(1);
+ // Limit query to the current record, unless it has the Versioned extension,
+ // in which case it requires special handling through augmentLoadLazyFields()
+ if (!isset($this->record['Version'])){
+ $dataQuery->where("\"$tableClass\".\"ID\" = {$this->record['ID']}")->limit(1);
+ }
$columns = array();
// Add SQL for fields, both simple & multi-value
@@ -2088,7 +2092,8 @@ protected function loadLazyFields($tableClass = null) {
}
if ($columns) {
- $query = $dataQuery->query(); // eh?
+ $query = $dataQuery->query();
+ $this->extend('augmentLoadLazyFields', $query, $dataQuery, $this->record);
$this->extend('augmentSQL', $query, $dataQuery);
$dataQuery->setQueriedColumns($columns);
View
15 model/Versioned.php
@@ -257,6 +257,21 @@ public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
. $dataQuery->getQueryParam('Versioned.mode'));
}
}
+
+ /**
+ * For lazy loaded fields requiring extra sql manipulation, ie versioning
+ * @param SQLQuery $query
+ * @param DataQuery $dataQuery
+ * @param array $record
+ */
+ function augmentLoadLazyFields(SQLQuery &$query, DataQuery &$dataQuery = null, $record) {
+ $dataClass = $dataQuery->dataClass();
+ if (isset($record['Version'])){
+ $dataQuery->where("\"$dataClass\".\"RecordID\" = " . $record['ID']);
+ $dataQuery->where("\"$dataClass\".\"Version\" = " . $record['Version']);
+ $dataQuery->setQueryParam('Versioned.mode', 'all_versions');
+ }
+ }
/**
* Keep track of the archive tables that have been created
View
39 tests/model/DataObjectLazyLoadingTest.php
@@ -6,9 +6,12 @@
class DataObjectLazyLoadingTest extends SapphireTest {
- static $fixture_file = 'DataObjectTest.yml';
+ static $fixture_file = array(
+ 'DataObjectTest.yml',
+ 'VersionedTest.yml'
+ );
- // These are all defined in DataObjectTest.php
+ // These are all defined in DataObjectTest.php and VersionedTest.php
protected $extraDataObjects = array(
'DataObjectTest_Team',
'DataObjectTest_Fixture',
@@ -18,7 +21,9 @@ class DataObjectLazyLoadingTest extends SapphireTest {
'DataObjectTest_FieldlessSubTable',
'DataObjectTest_ValidatedObject',
'DataObjectTest_Player',
- 'DataObjectTest_TeamComment'
+ 'DataObjectTest_TeamComment',
+ 'VersionedTest_DataObject',
+ 'VersionedTest_Subclass'
);
public function testQueriedColumnsID() {
@@ -244,4 +249,32 @@ public function testLazyLoadedFieldsGetAllFields() {
$this->assertArrayNotHasKey('SubclassDatabaseField_Lazy', $subteam1Lazy->toMap());
$this->assertArrayHasKey('SubclassDatabaseField', $subteam1Lazy->toMap());
}
+
+ public function testLazyLoadedFieldsOnVersionedRecords() {
+ // Save another record, sanity check that we're getting the right one
+ $obj2 = new VersionedTest_Subclass();
+ $obj2->Name = "test2";
+ $obj2->ExtraField = "foo2";
+ $obj2->write();
+
+ // Save the actual inspected record
+ $obj1 = new VersionedTest_Subclass();
+ $obj1->Name = "test";
+ $obj1->ExtraField = "foo";
+ $obj1->write();
+ $version1 = $obj1->Version;
+ $obj1->Name = "test2";
+ $obj1->ExtraField = "baz";
+ $obj1->write();
+ $version2 = $obj1->Version;
+
+ $reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version1);
+ $this->assertEquals($reloaded->Name, 'test');
+ $this->assertEquals($reloaded->ExtraField, 'foo');
+
+ $reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version2);
+ $this->assertEquals($reloaded->Name, 'test2');
+ $this->assertEquals($reloaded->ExtraField, 'baz');
+ $obj1->delete();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.