Skip to content

Commit

Permalink
Add and use Table::aliasField() where it makes sense.
Browse files Browse the repository at this point in the history
Adding this method came up in #6086 and it was simple enough to
implement.
  • Loading branch information
markstory committed Mar 20, 2015
1 parent b0f37f6 commit bf45a46
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -199,12 +199,11 @@ public function beforeFind(Event $event, Query $query, $options)

$conditions = function ($field, $locale, $query, $select) {
return function ($q) use ($field, $locale, $query, $select) {
$q->where([$q->repository()->alias() . '.locale' => $locale]);
$alias = $this->_table->alias();
$q->where([$q->repository()->aliasField('locale') => $locale]);

if ($query->autoFields() ||
in_array($field, $select, true) ||
in_array("$alias.$field", $select, true)
in_array($this->_table->aliasField($field), $select, true)
) {
$q->select(['id', 'content']);
}
Expand Down
18 changes: 9 additions & 9 deletions src/ORM/Behavior/TreeBehavior.php
Expand Up @@ -347,10 +347,9 @@ public function findPath(Query $query, array $options)
}

$config = $this->config();
$alias = $this->_table->alias();
list($left, $right) = array_map(
function ($field) use ($alias) {
return "$alias.$field";
function ($field) {
return $this->_table->aliasField($field);
},
[$config['left'], $config['right']]
);
Expand All @@ -375,8 +374,7 @@ function ($field) use ($alias) {
public function childCount(Entity $node, $direct = false)
{
$config = $this->config();
$alias = $this->_table->alias();
$parent = $alias . '.' . $config['parent'];
$parent = $this->_table->aliasField($config['parent']);

if ($direct) {
return $this->_scope($this->_table->find())
Expand Down Expand Up @@ -407,11 +405,10 @@ public function childCount(Entity $node, $direct = false)
public function findChildren(Query $query, array $options)
{
$config = $this->config();
$alias = $this->_table->alias();
$options += ['for' => null, 'direct' => false];
list($parent, $left, $right) = array_map(
function ($field) use ($alias) {
return "$alias.$field";
function ($field) {
return $this->_table->aliasField($field);
},
[$config['parent'], $config['left'], $config['right']]
);
Expand Down Expand Up @@ -458,7 +455,10 @@ function ($field) use ($alias) {
public function findTreeList(Query $query, array $options)
{
return $this->_scope($query)
->find('threaded', ['parentField' => $this->config()['parent'], 'order' => [$this->config()['left'] => 'ASC']])
->find('threaded', [
'parentField' => $this->config('parent'),
'order' => [$this->config('left') => 'ASC']
])
->formatResults(function ($results) use ($options) {
$options += [
'keyPath' => $this->_getPrimaryKey(),
Expand Down
11 changes: 11 additions & 0 deletions src/ORM/Table.php
Expand Up @@ -358,6 +358,17 @@ public function alias($alias = null)
return $this->_alias;
}

/**
* Alias a field with the table's current alias.
*
* @param string $field The field to alias.
* @return string The field prefixed with the table alias.
*/
public function aliasField($field)
{
return $this->alias() . '.' . $field;
}

/**
* Returns the table registry key used to create this table instance
*
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -161,6 +161,17 @@ public function testAliasMethod()
$this->assertEquals('AnotherOne', $table->alias());
}

/**
* Test that aliasField() works.
*
* @return void
*/
public function testAliasField()
{
$table = new Table(['alias' => 'Users']);
$this->assertEquals('Users.id', $table->aliasField('id'));
}

/**
* Tests connection method
*
Expand Down

0 comments on commit bf45a46

Please sign in to comment.