Skip to content

Commit

Permalink
Modify getRelationCountHash to prevent changing the counter and provi…
Browse files Browse the repository at this point in the history
…de a way to predict the hash
  • Loading branch information
khalilst committed Nov 27, 2020
1 parent 318c226 commit 8c62084
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Expand Up @@ -383,8 +383,10 @@ public function withAggregate($relations, $column, $function = null)
$relation = $this->getRelationWithoutConstraints($name);

if ($function) {
$predictedColumn = $this->getQuery()->from === $relation->getQuery()->getQuery()->from ? "{$relation->getRelationCountHash(true)}.$column" : $column;

$expression = sprintf('%s(%s)', $function, $this->getQuery()->getGrammar()->wrap(
$column === '*' ? $column : $relation->getRelated()->qualifyColumn($column)
$column === '*' ? $column : $relation->getRelated()->qualifyColumn($predictedColumn)
));
} else {
$expression = $column;
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Expand Up @@ -282,11 +282,12 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder
/**
* Get a relationship join table hash.
*
* @param bool $fixed
* @return string
*/
public function getRelationCountHash()
public function getRelationCountHash($fixed = false)
{
return 'laravel_reserved_'.static::$selfJoinCount++;
return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Expand Up @@ -1170,11 +1170,12 @@ public function getExistenceCompareKey()
/**
* Get a relationship join table hash.
*
* @param bool $fixed
* @return string
*/
public function getRelationCountHash()
public function getRelationCountHash($fixed = false)
{
return 'laravel_reserved_'.static::$selfJoinCount++;
return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Expand Up @@ -599,11 +599,12 @@ public function getRelationExistenceQueryForThroughSelfRelation(Builder $query,
/**
* Get a relationship join table hash.
*
* @param bool $fixed
* @return string
*/
public function getRelationCountHash()
public function getRelationCountHash($fixed = false)
{
return 'laravel_reserved_'.static::$selfJoinCount++;
return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Expand Up @@ -366,11 +366,12 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder
/**
* Get a relationship join table hash.
*
* @param bool $fixed
* @return string
*/
public function getRelationCountHash()
public function getRelationCountHash($fixed = false)
{
return 'laravel_reserved_'.static::$selfJoinCount++;
return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++);
}

/**
Expand Down

0 comments on commit 8c62084

Please sign in to comment.