mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX #2753: Couldn't have fields named the same as methods
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63432 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ba85bc7614
commit
ca884b809d
@ -702,18 +702,12 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
||||
$classSingleton = singleton($class);
|
||||
foreach($this->record as $fieldName => $fieldValue) {
|
||||
if(isset($this->changed[$fieldName]) && $this->changed[$fieldName] && $fieldType = $classSingleton->hasOwnTableDatabaseField($fieldName)) {
|
||||
$fieldObj = $this->obj($fieldName);
|
||||
$fieldObj = $this->dbObject($fieldName);
|
||||
if(!isset($manipulation[$class])) $manipulation[$class] = array();
|
||||
|
||||
// if database column doesn't correlate to a DBField instance...
|
||||
if(!$fieldObj) {
|
||||
// Set up a default Int field for relations
|
||||
if(preg_match('/ID$/', $fieldName) && $this->has_one(substr($fieldName,0,-2))) {
|
||||
$fieldObj = DBField::create('Int', $this->record[$fieldName], $fieldName);
|
||||
// Otherwise set up a default Varchar field
|
||||
} else {
|
||||
$fieldObj = DBField::create('Varchar', $this->record[$fieldName], $fieldName);
|
||||
}
|
||||
$fieldObj = DBField::create('Varchar', $this->record[$fieldName], $fieldName);
|
||||
}
|
||||
|
||||
// CompositeDBFields handle their own value storage; regular fields need to be
|
||||
@ -1884,28 +1878,30 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
||||
|
||||
/**
|
||||
* Return the DBField object that represents the given field.
|
||||
* This works similarly to obj() but still returns an object even when the field has no value.
|
||||
* This works similarly to obj() with 2 key differences:
|
||||
* - it still returns an object even when the field has no value.
|
||||
* - it only matches fields and not methods
|
||||
* - it matches foreign keys generated by has_one relationships, eg, "ParentID"
|
||||
*
|
||||
* @param string $fieldName Name of the field
|
||||
*
|
||||
* @return DBField The field as a DBField object
|
||||
*/
|
||||
public function dbObject($fieldName) {
|
||||
// Special case for ID field
|
||||
if($fieldName == 'ID') {
|
||||
return new PrimaryKey($fieldName, $this);
|
||||
} else {
|
||||
return $this->obj($fieldName);
|
||||
}
|
||||
/*
|
||||
$helperPair = $this->castingHelperPair($fieldName);
|
||||
$constructor = $helperPair['castingHelper'];
|
||||
|
||||
if($obj = eval($constructor)) {
|
||||
|
||||
// General casting information for items in $db or $casting
|
||||
} else if($helperPair = $this->castingHelperPair($fieldName)) {
|
||||
$constructor = $helperPair['castingHelper'];
|
||||
$obj = eval($constructor);
|
||||
$obj->setValue($this->$fieldName, $this->record);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
*/
|
||||
|
||||
// Special case for has_one relationships
|
||||
} else if(preg_match('/ID$/', $fieldName) && $this->has_one(substr($fieldName,0,-2))) {
|
||||
return DBField::create('Int', $this->record[$fieldName], $fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1999,7 +1995,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
||||
if($databaseFields) foreach($databaseFields as $k => $v) {
|
||||
if(!in_array($k, array('ClassName', 'LastEdited', 'Created'))) {
|
||||
if(ClassInfo::classImplements($v, 'CompositeDBField')) {
|
||||
$this->obj($k)->addToQuery($query);
|
||||
$this->dbObject($k)->addToQuery($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2015,7 +2011,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
||||
if($databaseFields) foreach($databaseFields as $k => $v) {
|
||||
if(!in_array($k, array('ClassName', 'LastEdited', 'Created'))) {
|
||||
if(ClassInfo::classImplements($v, 'CompositeDBField')) {
|
||||
$SNG->obj($k)->addToQuery($query);
|
||||
$SNG->dbObject($k)->addToQuery($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -289,6 +289,20 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $existingTeam->ID")->value());
|
||||
}
|
||||
|
||||
function testFieldNamesThatMatchMethodNamesWork() {
|
||||
/* Check that a field name that corresponds to a method on DataObject will still work */
|
||||
$obj = new DataObjectTest_FunnyFieldNames();
|
||||
$obj->Data = "value1";
|
||||
$obj->DbObject = "value2";
|
||||
$obj->Duplicate = "value3";
|
||||
$obj->write();
|
||||
|
||||
$this->assertNotNull($obj->ID);
|
||||
$this->assertEquals('value1', DB::query("SELECT Data FROM DataObjectTest_FunnyFieldNames WHERE ID = $obj->ID")->value());
|
||||
$this->assertEquals('value2', DB::query("SELECT DbObject FROM DataObjectTest_FunnyFieldNames WHERE ID = $obj->ID")->value());
|
||||
$this->assertEquals('value3', DB::query("SELECT Duplicate FROM DataObjectTest_FunnyFieldNames WHERE ID = $obj->ID")->value());
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Re-enable all test cases for field existence after behaviour has been fixed
|
||||
*/
|
||||
@ -446,6 +460,14 @@ class DataObjectTest_Team extends DataObject implements TestOnly {
|
||||
|
||||
}
|
||||
|
||||
class DataObjectTest_FunnyFieldNames extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
'Data' => 'Text',
|
||||
'Duplicate' => 'Text',
|
||||
'DbObject' => 'Text',
|
||||
);
|
||||
}
|
||||
|
||||
class DataObjectTest_SubTeam extends DataObjectTest_Team implements TestOnly {
|
||||
static $db = array(
|
||||
'SubclassDatabaseField' => 'Text'
|
||||
|
Loading…
Reference in New Issue
Block a user