diff --git a/model/DataObject.php b/model/DataObject.php index 8b8cb300a..ca972f6b2 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -1320,7 +1320,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $result = new HasManyList($componentClass, $joinField); if($this->model) $result->setModel($this->model); - if($this->ID) $result->setForeignID($this->ID); + $result->setForeignID($this->ID); $result = $result->where($filter)->limit($limit)->sort($sort); if($join) $result = $result->join($join); @@ -1412,7 +1412,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity // If this is called on a singleton, then we return an 'orphaned relation' that can have the // foreignID set elsewhere. - if($this->ID) $result->setForeignID($this->ID); + $result->setForeignID($this->ID); return $result->where($filter)->sort($sort)->limit($limit); } diff --git a/model/HasManyList.php b/model/HasManyList.php index 5be23c927..3d38bb52e 100644 --- a/model/HasManyList.php +++ b/model/HasManyList.php @@ -26,7 +26,7 @@ class HasManyList extends RelationList { if(is_array($this->foreignID)) { return "\"$this->foreignKey\" IN ('" . implode("', '", array_map('Convert::raw2sql', $this->foreignID)) . "')"; - } else if($this->foreignID){ + } else if($this->foreignID !== null){ return "\"$this->foreignKey\" = '" . Convert::raw2sql($this->foreignID) . "'"; } diff --git a/model/ManyManyList.php b/model/ManyManyList.php index 76e4db2b8..5ddb2b053 100644 --- a/model/ManyManyList.php +++ b/model/ManyManyList.php @@ -73,7 +73,7 @@ class ManyManyList extends RelationList { if(is_array($this->foreignID)) { return "\"$this->joinTable\".\"$this->foreignKey\" IN ('" . implode("', '", array_map('Convert::raw2sql', $this->foreignID)) . "')"; - } else if($this->foreignID){ + } else if($this->foreignID !== null){ return "\"$this->joinTable\".\"$this->foreignKey\" = '" . Convert::raw2sql($this->foreignID) . "'"; } diff --git a/security/Member.php b/security/Member.php index 5203e28ab..b78eefb53 100644 --- a/security/Member.php +++ b/security/Member.php @@ -946,7 +946,7 @@ class Member extends DataObject { */ public function Groups() { $groups = new Member_GroupSet('Group', 'Group_Members', 'GroupID', 'MemberID'); - if($this->ID) $groups->setForeignID($this->ID); + $groups->setForeignID($this->ID); $this->extend('updateGroups', $groups); diff --git a/tests/model/HasManyListTest.php b/tests/model/HasManyListTest.php new file mode 100644 index 000000000..f9394d4de --- /dev/null +++ b/tests/model/HasManyListTest.php @@ -0,0 +1,20 @@ +assertEquals(array(), $newTeam->Comments()->column('ID')); + } + +} \ No newline at end of file diff --git a/tests/model/ManyManyListTest.php b/tests/model/ManyManyListTest.php index f9240eb02..158f095f7 100644 --- a/tests/model/ManyManyListTest.php +++ b/tests/model/ManyManyListTest.php @@ -15,6 +15,12 @@ class ManyManyListTest extends SapphireTest { $list = ManyManyList::create('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); $this->assertEquals(2, $list->count()); } + + public function testRelationshipEmptyOnNewRecords() { + // Relies on the fact that (unrelated) teams exist in the fixture file already + $newPlayer = new DataObjectTest_Player(); // many_many Teams + $this->assertEquals(array(), $newPlayer->Teams()->column('ID')); + } public function testAddingSingleDataObjectByReference() { $player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');