BUG Fix regressions in CMS from db field changes

This commit is contained in:
Damian Mooyman 2015-09-28 17:22:44 +13:00
parent 60e75cbd99
commit 8e3f549b19
3 changed files with 51 additions and 40 deletions

View File

@ -16,6 +16,8 @@ class VirtualPage extends Page {
* Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list.
*/ */
private static $non_virtual_fields = array( private static $non_virtual_fields = array(
"ID",
"ClassName",
"SecurityTypeID", "SecurityTypeID",
"OwnerID", "OwnerID",
"URLSegment", "URLSegment",
@ -53,26 +55,30 @@ class VirtualPage extends Page {
$nonVirtualFields = array_merge(self::config()->non_virtual_fields, self::config()->initially_copied_fields); $nonVirtualFields = array_merge(self::config()->non_virtual_fields, self::config()->initially_copied_fields);
$record = $this->CopyContentFrom(); $record = $this->CopyContentFrom();
$allFields = $record->db();
if($hasOne = $record->hasOne()) foreach($hasOne as $link) $allFields[$link . 'ID'] = "Int";
$virtualFields = array(); $virtualFields = array();
foreach($allFields as $field => $type) { foreach($record->db() as $field => $type) {
if(!in_array($field, $nonVirtualFields)) $virtualFields[] = $field; if(!in_array($field, $nonVirtualFields)) {
$virtualFields[] = $field;
}
} }
return $virtualFields; return $virtualFields;
} }
/** /**
* @return SiteTree Returns the linked page, or failing that, a new object. * Returns the linked page, or failing that, a new object.
*
* Always returns a non-empty object
*
* @return SiteTree
*/ */
public function CopyContentFrom() { public function CopyContentFrom() {
$copyContentFromID = $this->CopyContentFromID; $copyContentFromID = $this->CopyContentFromID;
if(!$copyContentFromID) return new SiteTree(); if(!$copyContentFromID) {
return new SiteTree();
}
if(!isset($this->components['CopyContentFrom'])) { if(!isset($this->components['CopyContentFrom'])) {
$this->components['CopyContentFrom'] = DataObject::get_by_id("SiteTree", $this->components['CopyContentFrom'] = DataObject::get_by_id("SiteTree", $copyContentFromID);
$copyContentFromID);
// Don't let VirtualPages point to other VirtualPages // Don't let VirtualPages point to other VirtualPages
if($this->components['CopyContentFrom'] instanceof VirtualPage) { if($this->components['CopyContentFrom'] instanceof VirtualPage) {
@ -87,8 +93,11 @@ class VirtualPage extends Page {
return $this->components['CopyContentFrom'] ? $this->components['CopyContentFrom'] : new SiteTree(); return $this->components['CopyContentFrom'] ? $this->components['CopyContentFrom'] : new SiteTree();
} }
public function setCopyContentFromID($val) { public function setCopyContentFromID($val) {
if($val && DataObject::get_by_id('SiteTree', $val) instanceof VirtualPage) $val = 0; if($val && DataObject::get_by_id('SiteTree', $val) instanceof VirtualPage) {
$val = 0;
}
return $this->setField("CopyContentFromID", $val); return $this->setField("CopyContentFromID", $val);
} }
@ -440,13 +449,10 @@ class VirtualPage extends Page {
* @return bool * @return bool
*/ */
public function hasField($field) { public function hasField($field) {
return ( if(parent::hasField($field)) {
array_key_exists($field, $this->record) return true;
|| $this->hasDatabaseField($field) }
|| array_key_exists($field, $this->db()) // Needed for composite fields return $this->CopyContentFrom()->hasField($field);
|| parent::hasMethod("get{$field}")
|| $this->CopyContentFrom()->hasField($field)
);
} }
/** /**
* Overwrite to also check for method on the original data object * Overwrite to also check for method on the original data object
@ -455,8 +461,10 @@ class VirtualPage extends Page {
* @return bool * @return bool
*/ */
public function hasMethod($method) { public function hasMethod($method) {
if(parent::hasMethod($method)) return true; if(parent::hasMethod($method)) {
return $this->copyContentFrom()->hasMethod($method); return true;
}
return $this->CopyContentFrom()->hasMethod($method);
} }
/** /**
@ -467,11 +475,9 @@ class VirtualPage extends Page {
* @return string * @return string
*/ */
public function castingHelper($field) { public function castingHelper($field) {
if($this->copyContentFrom()) { return $this
return $this->copyContentFrom()->castingHelper($field); ->CopyContentFrom()
} else { ->castingHelper($field);
return parent::castingHelper($field);
}
} }
} }

View File

@ -6,8 +6,12 @@ class VirtualPageTest extends SapphireTest {
protected $extraDataObjects = array( protected $extraDataObjects = array(
'VirtualPageTest_ClassA', 'VirtualPageTest_ClassA',
'VirtualPageTest_ClassB', 'VirtualPageTest_ClassB',
'VirtualPageTest_ClassC',
'VirtualPageTest_NotRoot',
'VirtualPageTest_PageExtension',
'VirtualPageTest_PageWithAllowedChildren',
'VirtualPageTest_TestDBField',
'VirtualPageTest_VirtualPageSub', 'VirtualPageTest_VirtualPageSub',
'VirtualPageTest_PageWithAllowedChildren'
); );
protected $illegalExtensions = array( protected $illegalExtensions = array(

View File

@ -9,9 +9,10 @@ Page:
Title: Sub-pages Title: Sub-pages
VirtualPage: VirtualPage:
vp1: vp1:
Title: vp1
CopyContentFrom: =>Page.master CopyContentFrom: =>Page.master
Parent: =>Page.holder Parent: =>Page.holder
vp2: vp2:
Title: vp2
CopyContentFrom: =>Page.master CopyContentFrom: =>Page.master
Parent: =>Page.holder Parent: =>Page.holder