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:
Ingo Schommer 2010-12-07 11:12:24 +13:00
parent 2b6357737b
commit 09283fba88
2 changed files with 25 additions and 5 deletions

View File

@ -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;

View File

@ -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 {