Skip to content

Commit

Permalink
Merge branch '4' into 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 9, 2023
2 parents 2c9beab + e28457c commit b01afbe
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ORM/DataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $colum
} else {
$query->selectField($quotedField, $k);
}
$dbO = Injector::inst()->create($v, $k);
$dbO->addToQuery($query);
}
}
foreach ($compositeFields as $k => $v) {
Expand Down
10 changes: 10 additions & 0 deletions tests/php/ORM/DataQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DataQueryTest extends SapphireTest
protected static $fixture_file = 'DataQueryTest.yml';

protected static $extra_dataobjects = [
DataQueryTest\DataObjectAddsToQuery::class,
DataQueryTest\ObjectA::class,
DataQueryTest\ObjectB::class,
DataQueryTest\ObjectC::class,
Expand Down Expand Up @@ -378,6 +379,15 @@ public function testComparisonClauseTextCaseSensitive()
static::resetDBSchema(true);
}

public function testAddToQueryIsCalled()
{
// Including filter on parent table only doesn't pull in second
$query = new DataQuery(DataQueryTest\DataObjectAddsToQuery::class);
$result = $query->getFinalisedQuery();
// The `DBFieldAddsToQuery` test field removes itself from the select query
$this->assertArrayNotHasKey('FieldTwo', $result->getSelect());
}

/**
* Tests that getFinalisedQuery can include all tables
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/php/ORM/DataQueryTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ SilverStripe\ORM\Tests\DataQueryTest\ObjectH:
SortOrder: 2
ManyTestEs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectE.query2
ManyTestIs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectI.query3

SilverStripe\ORM\Tests\DataQueryTest\DataObjectAddsToQuery:
obj1:
FieldOne: 'This is a value'
FieldTwo: 'This is also a value'
16 changes: 16 additions & 0 deletions tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SilverStripe\ORM\Tests\DataQueryTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\FieldType\DBText;

class DBFieldAddsToQuery extends DBText implements TestOnly
{
public function addToQuery(&$query)
{
$select = $query->getSelect();
unset($select[$this->name]);
$query->setSelect($select);
}
}
16 changes: 16 additions & 0 deletions tests/php/ORM/DataQueryTest/DataObjectAddsToQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SilverStripe\ORM\Tests\DataQueryTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class DataObjectAddsToQuery extends DataObject implements TestOnly
{
private static $table_name = 'DataQueryTest_AddsToQuery';

private static $db = [
'FieldOne' => 'Text',
'FieldTwo' => DBFieldAddsToQuery::class,
];
}

0 comments on commit b01afbe

Please sign in to comment.