Skip to content

Commit

Permalink
Merge 0c230ca into fb199b4
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Jul 4, 2020
2 parents fb199b4 + 0c230ca commit 9e1833c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
5 changes: 4 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,7 @@ parameters:
message: "#^Access to an undefined property Cake\\\\Mailer\\\\Renderer\\:\\:\\$response\\.$#"
count: 1
path: src/Mailer/Renderer.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/ORM\Query.php
39 changes: 34 additions & 5 deletions src/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
*/
protected $_hydrate = true;

/**
* Whether aliases are generated for fields.
*
* @var bool
*/
protected $aliasingEnabled = true;

/**
* A callable function that can be used to calculate the total amount of
* records this query will match when not using `limit`
Expand Down Expand Up @@ -225,7 +232,11 @@ public function select($fields = [], bool $overwrite = false)
}

if ($fields instanceof Table) {
$fields = $this->aliasFields($fields->getSchema()->columns(), $fields->getAlias());
if ($this->aliasingEnabled) {
$fields = $this->aliasFields($fields->getSchema()->columns(), $fields->getAlias());
} else {
$fields = $fields->getSchema()->columns();
}
}

return parent::select($fields, $overwrite);
Expand Down Expand Up @@ -255,9 +266,11 @@ public function selectAllExcept($table, array $excludedFields, bool $overwrite =
}

$fields = array_diff($table->getSchema()->columns(), $excludedFields);
$aliasedFields = $this->aliasFields($fields);
if ($this->aliasingEnabled) {
$fields = $this->aliasFields($fields);
}

return $this->select($aliasedFields, $overwrite);
return $this->select($fields, $overwrite);
}

/**
Expand Down Expand Up @@ -1163,8 +1176,10 @@ protected function _addDefaultFields(): void
$select = $this->clause('select');
}

$aliased = $this->aliasFields($select, $repository->getAlias());
$this->select($aliased, true);
if ($this->aliasingEnabled) {
$select = $this->aliasFields($select, $repository->getAlias());
}
$this->select($select, true);
}

/**
Expand Down Expand Up @@ -1285,6 +1300,20 @@ public function insert(array $columns, array $types = [])
return parent::insert($columns, $types);
}

/**
* Returns a new Query that has automatic field aliasing disabled.
*
* @param \Cake\ORM\Table $table The table this query is starting on
* @return static
*/
public function subquery(Table $table)
{
$query = new static($this->getConnection(), $table);
$query->aliasingEnabled = false;

return $query;
}

/**
* {@inheritDoc}
*
Expand Down
35 changes: 35 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4001,4 +4001,39 @@ public function testWith(): void

$this->assertEquals($expected, $query->toArray());
}

/**
* Tests subquery() copies connection by default.
*
* @return void
*/
public function testSubqueryConnection()
{
$subquery = (new Query($this->connection, $this->table))->subquery($this->table);
$this->assertEquals($this->connection, $subquery->getConnection());
}

/**
* Tests subquery() disables aliasing.
*
* @return void
*/
public function testSubqueryAliasing()
{
$articles = $this->getTableLocator()->get('Articles');
$subquery = (new Query($this->connection, $this->table))->subquery($articles);

$subquery->select('Articles.field1');
$this->assertRegExpSql(
'SELECT <Articles>.<field1> FROM <articles> <Articles>',
$subquery->sql(),
!$this->connection->getDriver()->isAutoQuotingEnabled()
);

$subquery->select($articles, true);
$this->assertEqualsSql('SELECT id, author_id, title, body, published FROM articles Articles', $subquery->sql());

$subquery->selectAllExcept($articles, ['author_id'], true);
$this->assertEqualsSql('SELECT id, title, body, published FROM articles Articles', $subquery->sql());
}
}

0 comments on commit 9e1833c

Please sign in to comment.