Skip to content

Commit 6701e9d

Browse files
Merge pull request #11 from ProAI/revert-8-master
Revert "Fix not unique table/alias"
2 parents be48baf + a698ec3 commit 6701e9d

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/VersioningScope.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ public function apply(Builder $builder, Model $model)
3333
$this->extend($builder);
3434
}
3535

36+
/**
37+
* Remove the scope from the given Eloquent query builder.
38+
*
39+
* @param \Illuminate\Database\Eloquent\Builder $builder
40+
* @param \Illuminate\Database\Eloquent\Model $model
41+
* @return void
42+
*/
43+
public function remove(Builder $builder, Model $model)
44+
{
45+
$table = $model->getVersionTable();
46+
47+
$query = $builder->getQuery();
48+
49+
$query->joins = collect($query->joins)->reject(function($join) use ($table)
50+
{
51+
return $this->isVersionJoinConstraint($join, $table);
52+
})->values()->all();
53+
}
54+
3655
/**
3756
* Extend the query builder with the needed functions.
3857
*
@@ -58,7 +77,9 @@ protected function addVersion(Builder $builder)
5877
$builder->macro('version', function(Builder $builder, $version) {
5978
$model = $builder->getModel();
6079

61-
$builder->withoutGlobalScope($this)->join($model->getVersionTable(), function($join) use ($model, $version) {
80+
$this->remove($builder, $builder->getModel());
81+
82+
$builder->join($model->getVersionTable(), function($join) use ($model, $version) {
6283
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
6384
$join->where($model->getQualifiedVersionColumn(), '=', $version);
6485
});
@@ -78,12 +99,26 @@ protected function addAllVersions(Builder $builder)
7899
$builder->macro('allVersions', function(Builder $builder) {
79100
$model = $builder->getModel();
80101

81-
$builder->withoutGlobalScope($this)->join($model->getVersionTable(), function($join) use ($model) {
102+
$this->remove($builder, $builder->getModel());
103+
104+
$builder->join($model->getVersionTable(), function($join) use ($model) {
82105
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
83106
});
84107

85108
return $builder;
86109
});
87110
}
88111

112+
/**
113+
* Determine if the given join clause is a version constraint.
114+
*
115+
* @param \Illuminate\Database\Query\JoinClause $join
116+
* @param string $column
117+
* @return bool
118+
*/
119+
protected function isVersionJoinConstraint(JoinClause $join, $table)
120+
{
121+
return $join->type == 'inner' && $join->table == $table;
122+
}
123+
89124
}

0 commit comments

Comments
 (0)