Skip to content

Commit

Permalink
Change the name of method getQueryTableName and remove its $query arg…
Browse files Browse the repository at this point in the history
…ument (yiisoft#12893)

* refactores getQueryTableName:
* replaces the $query argument with a $this implementation

* exposes getQueryTableName to be public instead of private. Fixes yiisoft#12878

* added unit tests for exposed method

* updated changelog

* - methodname changed to 'getTableNameAndAlias'
- scope back to private
- added @internal tag to emphasize that the method is used purely for the internal workings of this piece of software.
- removed changelog (as the API has not changed)

* update tests
  • Loading branch information
dynasource authored and cebe committed Dec 8, 2016
1 parent 65e4a28 commit 92eee10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
18 changes: 9 additions & 9 deletions framework/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function prepare($builder)
}

if (empty($this->select) && !empty($this->join)) {
list(, $alias) = $this->getQueryTableName($this);
list(, $alias) = $this->getTableNameAndAlias();
$this->select = ["$alias.*"];
}

Expand Down Expand Up @@ -551,18 +551,18 @@ private function getJoinType($joinType, $name)

/**
* Returns the table name and the table alias for [[modelClass]].
* @param ActiveQuery $query
* @return array the table name and the table alias.
* @internal
*/
private function getQueryTableName($query)
private function getTableNameAndAlias()
{
if (empty($query->from)) {
if (empty($this->from)) {
/* @var $modelClass ActiveRecord */
$modelClass = $query->modelClass;
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
} else {
$tableName = '';
foreach ($query->from as $alias => $tableName) {
foreach ($this->from as $alias => $tableName) {
if (is_string($alias)) {
return [$tableName, $alias];
} else {
Expand Down Expand Up @@ -603,8 +603,8 @@ private function joinWithRelation($parent, $child, $joinType)
return;
}

list ($parentTable, $parentAlias) = $this->getQueryTableName($parent);
list ($childTable, $childAlias) = $this->getQueryTableName($child);
list ($parentTable, $parentAlias) = $parent->getTableNameAndAlias();
list ($childTable, $childAlias) = $child->getTableNameAndAlias();

if (!empty($child->link)) {

Expand Down Expand Up @@ -778,7 +778,7 @@ public function viaTable($tableName, $link, callable $callable = null)
public function alias($alias)
{
if (empty($this->from) || count($this->from) < 2) {
list($tableName, ) = $this->getQueryTableName($this);
list($tableName, ) = $this->getTableNameAndAlias();
$this->from = [$alias => $tableName];
} else {
/* @var $modelClass ActiveRecord */
Expand Down
28 changes: 23 additions & 5 deletions tests/framework/db/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testJoinWith()
}

/**
* @todo: tests for internal logic of joinWith()
* @todo: tests for internal logic of innerJoinWith()
*/
public function testInnerJoinWith()
{
Expand All @@ -129,6 +129,24 @@ public function testInnerJoinWith()
], $result->joinWith);
}

/**
* @todo: tests for the regex inside getQueryTableName
*/
public function testGetQueryTableName_from_not_set()
{
$query = new ActiveQuery(Customer::className());
$result = $this->invokeMethod($query,'getTableNameAndAlias');
$this->assertEquals(['customer','customer'], $result);
}

public function testGetQueryTableName_from_set()
{
$options = ['from' => ['alias'=>'customer']];
$query = new ActiveQuery(Customer::className(),$options);
$result = $this->invokeMethod($query,'getTableNameAndAlias');
$this->assertEquals(['customer','alias'], $result);
}

public function testOnCondition()
{
$query = new ActiveQuery(Customer::className());
Expand All @@ -139,7 +157,7 @@ public function testOnCondition()
$this->assertEquals($params, $result->params);
}

public function testAndOnCondition_OnIsNull()
public function testAndOnCondition_on_not_set()
{
$query = new ActiveQuery(Customer::className());
$on = ['active' => true];
Expand All @@ -149,7 +167,7 @@ public function testAndOnCondition_OnIsNull()
$this->assertEquals($params, $result->params);
}

public function testAndOnCondition_OnIsNotNull()
public function testAndOnCondition_on_set()
{
$onOld = ['active' => true];
$query = new ActiveQuery(Customer::className());
Expand All @@ -162,7 +180,7 @@ public function testAndOnCondition_OnIsNotNull()
$this->assertEquals($params, $result->params);
}

public function testOrOnCondition_OnIsNull()
public function testOrOnCondition_on_not_set()
{
$query = new ActiveQuery(Customer::className());
$on = ['active' => true];
Expand All @@ -172,7 +190,7 @@ public function testOrOnCondition_OnIsNull()
$this->assertEquals($params, $result->params);
}

public function testOrOnCondition_OnIsNotNull()
public function testOrOnCondition_on_set()
{
$onOld = ['active' => true];
$query = new ActiveQuery(Customer::className());
Expand Down

0 comments on commit 92eee10

Please sign in to comment.