Skip to content

Fix not unique table/alias #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions src/VersioningScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@ public function apply(Builder $builder, Model $model)
$this->extend($builder);
}

/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function remove(Builder $builder, Model $model)
{
$table = $model->getVersionTable();

$query = $builder->getQuery();

$query->joins = collect($query->joins)->reject(function($join) use ($table)
{
return $this->isVersionJoinConstraint($join, $table);
})->values()->all();
}

/**
* Extend the query builder with the needed functions.
*
Expand All @@ -77,9 +58,7 @@ protected function addVersion(Builder $builder)
$builder->macro('version', function(Builder $builder, $version) {
$model = $builder->getModel();

$this->remove($builder, $builder->getModel());

$builder->join($model->getVersionTable(), function($join) use ($model, $version) {
$builder->withoutGlobalScope($this)->join($model->getVersionTable(), function($join) use ($model, $version) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
$join->where($model->getQualifiedVersionColumn(), '=', $version);
});
Expand All @@ -99,26 +78,12 @@ protected function addAllVersions(Builder $builder)
$builder->macro('allVersions', function(Builder $builder) {
$model = $builder->getModel();

$this->remove($builder, $builder->getModel());

$builder->join($model->getVersionTable(), function($join) use ($model) {
$builder->withoutGlobalScope($this)->join($model->getVersionTable(), function($join) use ($model) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
});

return $builder;
});
}

/**
* Determine if the given join clause is a version constraint.
*
* @param \Illuminate\Database\Query\JoinClause $join
* @param string $column
* @return bool
*/
protected function isVersionJoinConstraint(JoinClause $join, $table)
{
return $join->type == 'inner' && $join->table == $table;
}

}