Merge pull request #3547 from kinglozzer/pulls/3546-apply-relation

Fix DataQuery::applyRelation for multiple relations of the same class (fixes #3546)
This commit is contained in:
Sam Minnée 2014-10-15 18:00:19 +13:00
commit 3d9fa205fe
2 changed files with 23 additions and 1 deletions

View File

@ -608,7 +608,8 @@ class DataQuery {
$model = singleton($modelClass); $model = singleton($modelClass);
if ($component = $model->has_one($rel)) { if ($component = $model->has_one($rel)) {
if(!$this->query->isJoinedTo($component)) { if(!$this->query->isJoinedTo($component)) {
$foreignKey = $model->getReverseAssociation($component); $has_one = array_flip($model->has_one());
$foreignKey = $has_one[$component];
$this->query->addLeftJoin($component, $this->query->addLeftJoin($component,
"\"$component\".\"ID\" = \"{$modelClass}\".\"{$foreignKey}ID\""); "\"$component\".\"ID\" = \"{$modelClass}\".\"{$foreignKey}ID\"");

View File

@ -681,6 +681,27 @@ class DataListTest extends SapphireTest {
$this->assertEquals(array_intersect($values, array('Joe', 'Bob')), $values); $this->assertEquals(array_intersect($values, array('Joe', 'Bob')), $values);
} }
public function testFilterOnImplicitJoin() {
// Many to many
$list = DataObjectTest_Team::get()
->filter('Players.FirstName', array('Captain', 'Captain 2'));
$this->assertEquals(2, $list->count());
// Has many
$list = DataObjectTest_Team::get()
->filter('Comments.Name', array('Joe', 'Phil'));
$this->assertEquals(2, $list->count());
// Has one
$list = DataObjectTest_Player::get()
->filter('FavouriteTeam.Title', 'Team 1');
$this->assertEquals(1, $list->count());
$this->assertEquals('007', $list->first()->ShirtNumber);
}
public function testFilterAndExcludeById() { public function testFilterAndExcludeById() {
$id = $this->idFromFixture('DataObjectTest_SubTeam', 'subteam1'); $id = $this->idFromFixture('DataObjectTest_SubTeam', 'subteam1');
$list = DataObjectTest_SubTeam::get()->filter('ID', $id); $list = DataObjectTest_SubTeam::get()->filter('ID', $id);