mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Fixed DataObject->isEmpty() to only inspect custom fields (excluding "ClassName" etc), and use DBField->hasValue() for more type-specific emptyness checks
This commit is contained in:
parent
2b6357737b
commit
09283fba88
@ -562,13 +562,25 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
return ($this->record && $this->record['ID'] > 0);
|
return ($this->record && $this->record['ID'] > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if all values (other than "ID") are
|
||||||
|
* considered empty (by weak boolean comparison).
|
||||||
|
* Only checks for fields listed in {@link custom_database_fields()}
|
||||||
|
*
|
||||||
|
* @todo Use DBField->hasValue()
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
public function isEmpty(){
|
public function isEmpty(){
|
||||||
$isEmpty = true;
|
$isEmpty = true;
|
||||||
if($this->record){
|
$customFields = self::custom_database_fields(get_class($this));
|
||||||
foreach($this->record as $k=>$v){
|
if($map = $this->toMap()){
|
||||||
if($k != "ID"){
|
foreach($map as $k=>$v){
|
||||||
$isEmpty = $isEmpty && !$v;
|
// only look at custom fields
|
||||||
}
|
if(!array_key_exists($k, $customFields)) continue;
|
||||||
|
|
||||||
|
$dbObj = ($v instanceof DBField) ? $v : $this->dbObject($k);
|
||||||
|
$isEmpty = ($isEmpty && !$dbObj->hasValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $isEmpty;
|
return $isEmpty;
|
||||||
|
@ -993,6 +993,14 @@ class DataObjectTest extends SapphireTest {
|
|||||||
$newObj = new DataObjectTest_SubTeam();
|
$newObj = new DataObjectTest_SubTeam();
|
||||||
$this->assertArrayHasKey('Title', $map, 'Contains null fields');
|
$this->assertArrayHasKey('Title', $map, 'Contains null fields');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testIsEmpty() {
|
||||||
|
$objEmpty = new DataObjectTest_Team();
|
||||||
|
$this->assertTrue($objEmpty->isEmpty(), 'New instance without populated defaults is empty');
|
||||||
|
|
||||||
|
$objEmpty->Title = '0'; //
|
||||||
|
$this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataObjectTest_Player extends Member implements TestOnly {
|
class DataObjectTest_Player extends Member implements TestOnly {
|
||||||
|
Loading…
Reference in New Issue
Block a user