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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(){
|
||||
$isEmpty = true;
|
||||
if($this->record){
|
||||
foreach($this->record as $k=>$v){
|
||||
if($k != "ID"){
|
||||
$isEmpty = $isEmpty && !$v;
|
||||
}
|
||||
$customFields = self::custom_database_fields(get_class($this));
|
||||
if($map = $this->toMap()){
|
||||
foreach($map as $k=>$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;
|
||||
|
@ -993,6 +993,14 @@ class DataObjectTest extends SapphireTest {
|
||||
$newObj = new DataObjectTest_SubTeam();
|
||||
$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 {
|
||||
|
Loading…
Reference in New Issue
Block a user