Merge pull request #1132 from kinglozzer/pulls/virtualpage-casting

FIX: VirtualPages use correct casting for 'virtual' database fields
This commit is contained in:
Damian Mooyman 2015-03-06 11:29:18 +13:00
commit cabd8bbab9
2 changed files with 36 additions and 0 deletions

View File

@ -450,6 +450,22 @@ class VirtualPage extends Page {
if(parent::hasMethod($method)) return true; if(parent::hasMethod($method)) return true;
return $this->copyContentFrom()->hasMethod($method); return $this->copyContentFrom()->hasMethod($method);
} }
/**
* Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field
* on this object.
*
* @param string $field
* @return string
*/
public function castingHelper($field) {
if($this->copyContentFrom()) {
return $this->copyContentFrom()->castingHelper($field);
} else {
return parent::castingHelper($field);
}
}
} }
/** /**

View File

@ -591,6 +591,19 @@ class VirtualPageTest extends SapphireTest {
'No field copying from previous original after page type changed' 'No field copying from previous original after page type changed'
); );
} }
public function testVirtualPageFindsCorrectCasting() {
$page = new VirtualPageTest_ClassA();
$page->CastingTest = "Some content";
$page->write();
$virtual = new VirtualPage();
$virtual->CopyContentFromID = $page->ID;
$virtual->write();
$this->assertEquals('VirtualPageTest_TestDBField', $virtual->castingHelper('CastingTest'));
$this->assertEquals('SOME CONTENT', $virtual->obj('CastingTest')->forTemplate());
}
} }
class VirtualPageTest_ClassA extends Page implements TestOnly { class VirtualPageTest_ClassA extends Page implements TestOnly {
@ -599,6 +612,7 @@ class VirtualPageTest_ClassA extends Page implements TestOnly {
'MyInitiallyCopiedField' => 'Text', 'MyInitiallyCopiedField' => 'Text',
'MyVirtualField' => 'Text', 'MyVirtualField' => 'Text',
'MyNonVirtualField' => 'Text', 'MyNonVirtualField' => 'Text',
'CastingTest' => 'VirtualPageTest_TestDBField'
); );
private static $allowed_children = array('VirtualPageTest_ClassB'); private static $allowed_children = array('VirtualPageTest_ClassB');
@ -616,6 +630,12 @@ class VirtualPageTest_NotRoot extends Page implements TestOnly {
private static $can_be_root = false; private static $can_be_root = false;
} }
class VirtualPageTest_TestDBField extends Varchar implements TestOnly {
public function forTemplate() {
return strtoupper($this->XML());
}
}
class VirtualPageTest_VirtualPageSub extends VirtualPage implements TestOnly { class VirtualPageTest_VirtualPageSub extends VirtualPage implements TestOnly {
private static $db = array( private static $db = array(
'MyProperty' => 'Varchar', 'MyProperty' => 'Varchar',