Skip to content

Commit

Permalink
[5.2] Stabilized table aliases for self joins by adding count (#13401)
Browse files Browse the repository at this point in the history
* stabilized table aliases for self joins by adding count, in order to improve query cacheability

* style fixes

* changed prefix to laravel_reserved_ for self join alias
  • Loading branch information
JeroenVanOort authored and taylorotwell committed May 9, 2016
1 parent 607a720 commit e86c4a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class BelongsTo extends Relation
*/
protected $relation;

/**
* The count of self joins.
*
* @var int
*/
protected static $selfJoinCount = 0;

/**
* Create a new belongs to relationship instance.
*
Expand Down Expand Up @@ -125,7 +132,7 @@ public function getRelationQueryForSelfRelation(Builder $query, Builder $parent,
*/
public function getRelationCountHash()
{
return 'self_'.md5(microtime(true));
return 'laravel_reserved_'.static::$selfJoinCount++;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class BelongsToMany extends Relation
*/
protected $pivotUpdatedAt;

/**
* The count of self joins.
*
* @var int
*/
protected static $selfJoinCount = 0;

/**
* Create a new belongs to many relationship instance.
*
Expand Down Expand Up @@ -376,7 +383,7 @@ public function getRelationQueryForSelfJoin(Builder $query, Builder $parent, $co
*/
public function getRelationCountHash()
{
return 'self_'.md5(microtime(true));
return 'laravel_reserved_'.static::$selfJoinCount++;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ abstract class HasOneOrMany extends Relation
*/
protected $localKey;

/**
* The count of self joins.
*
* @var int
*/
protected static $selfJoinCount = 0;

/**
* Create a new has one or many relationship instance.
*
Expand Down Expand Up @@ -99,7 +106,7 @@ public function getRelationQueryForSelfRelation(Builder $query, Builder $parent,
*/
public function getRelationCountHash()
{
return 'self_'.md5(microtime(true));
return 'laravel_reserved_'.static::$selfJoinCount++;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public function testSelfHasNested()

// alias has a dynamic hash, so replace with a static string for comparison
$alias = 'self_alias_hash';
$aliasRegex = '/\b(self_[a-f0-9]{32})(\b|$)/i';
$aliasRegex = '/\b(laravel_reserved_\d)(\b|$)/i';

$nestedSql = preg_replace($aliasRegex, $alias, $nestedSql);
$dotSql = preg_replace($aliasRegex, $alias, $dotSql);
Expand All @@ -569,7 +569,7 @@ public function testSelfHasNestedUsesAlias()

// alias has a dynamic hash, so replace with a static string for comparison
$alias = 'self_alias_hash';
$aliasRegex = '/\b(self_[a-f0-9]{32})(\b|$)/i';
$aliasRegex = '/\b(laravel_reserved_\d)(\b|$)/i';

$sql = preg_replace($aliasRegex, $alias, $sql);

Expand Down

0 comments on commit e86c4a4

Please sign in to comment.