From 09283fba88f8c2cc3e30546dc60f7be2e8c5a190 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 7 Dec 2010 11:12:24 +1300 Subject: [PATCH] BUGFIX Fixed DataObject->isEmpty() to only inspect custom fields (excluding "ClassName" etc), and use DBField->hasValue() for more type-specific emptyness checks --- core/model/DataObject.php | 22 +++++++++++++++++----- tests/DataObjectTest.php | 8 ++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 18ddedf1d..80eb7c8fe 100755 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -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; diff --git a/tests/DataObjectTest.php b/tests/DataObjectTest.php index 62a418537..465ad2416 100755 --- a/tests/DataObjectTest.php +++ b/tests/DataObjectTest.php @@ -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 {