BUGFIX Fixed HasManyList and ManyManyList queries for relationships on new records (was returning all available records due to the SQL filtering ignoring ID=0)

This commit is contained in:
Ingo Schommer 2012-03-05 23:59:27 +01:00
parent e0bd5d1070
commit bb6d4c506e
6 changed files with 31 additions and 5 deletions

View File

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

View File

@ -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) . "'";
}

View File

@ -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) . "'";
}

View File

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

View File

@ -0,0 +1,20 @@
<?php
class HasManyListTest extends SapphireTest {
// Borrow the model from DataObjectTest
public static $fixture_file = 'DataObjectTest.yml';
protected $extraDataObjects = array(
'DataObjectTest_Team',
'DataObjectTest_SubTeam',
'DataObjectTest_Player',
);
public function testRelationshipEmptyOnNewRecords() {
// Relies on the fact that (unrelated) comments exist in the fixture file already
$newTeam = new DataObjectTest_Team(); // has_many Comments
$this->assertEquals(array(), $newTeam->Comments()->column('ID'));
}
}

View File

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