Skip to content

Commit

Permalink
improved table aliasing on whereHasBuilder to enforce a table aliases…
Browse files Browse the repository at this point in the history
… down the tree
  • Loading branch information
Alex Blake committed Mar 29, 2018
1 parent d71b1b5 commit 43e9035
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 156 deletions.
65 changes: 32 additions & 33 deletions src/Concerns/JoinsToModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait JoinsToModel


private function joinsToModel() {
return function($relation, $from, $dd = false) {
return function($relation, $from) {

/**
* Wrap the attributes of the give JSON path.
Expand All @@ -33,11 +33,6 @@ private function joinsToModel() {
}
$relation = $from->getRelationWithoutConstraints($relation);


if($dd) {
// dd($this);
}

$parent = $relation->getParent();
if(property_exists($this, 'priorJoinToModelParents')) {
if(array_key_exists($parent->getTable(), $this->priorJoinToModelParents)) {
Expand All @@ -48,22 +43,6 @@ private function joinsToModel() {
}
$related = $relation->getRelated();
switch(get_class($relation)) {
case 'Illuminate\Database\Eloquent\Relations\BelongsToMany':
// For BelongsToMany
$query = $relation->getQuery()->getQuery();
$hash = $relation->getRelationCountHash();
$table = $relation->getTable();
$FK = $relation->getQualifiedForeignPivotKeyName();
$query->from($table);

// handle self relations
// if(get_class($relation->getParent()) === get_class($relation->getRelated())) {
$query->from($table.' as '.$hash);
$related->setTable($hash);
$FK = str_replace($table, $hash, $FK);
// }

break;
case 'Illuminate\Database\Eloquent\Relations\BelongsTo':
// For HasOneOrMany
$query = $relation->getQuery()->getQuery();
Expand All @@ -75,35 +54,55 @@ private function joinsToModel() {
// if(get_class($relation->getParent()) === get_class($relation->getRelated())) {
$query->from($table.' as '.$hash);
$related->setTable($hash);
$FK = str_replace($table, $hash, $FK);
$FK = str_replace($table.'.', $hash.'.', $FK);
// }

$PK = $relation->getQualifiedOwnerKeyName();
break;
default:
case 'Illuminate\Database\Eloquent\Relations\HasOne':
// For HasOneOrMany
$query = $relation->getQuery()->getQuery();
$hash = $relation->getRelationCountHash();
$table = $related->getTable();
// dd(get_class_methods($relation));
$FK = $relation->getQualifiedForeignKeyName();

// handle self relations
// if(get_class($relation->getParent()) === get_class($relation->getRelated())) {
$query->from($table.' as '.$hash);
$related->setTable($hash);
$FK = str_replace($table, $hash, $FK);
$FK = str_replace($table.'.', $hash.'.', $FK);
// }
}
$PK = $relation->getQualifiedParentKeyName();
break;
// case 'Illuminate\Database\Eloquent\Relations\BelongsToMany':
// // For BelongsToMany
// $query = $relation->getQuery()->getQuery();
// $hash = $relation->getRelationCountHash();
// $table = $relation->getTable();

$this->leftJoin($query->from, $relation->getQualifiedOwnerKeyName(), '=', $FK);
$this->priorJoinToModelParents[$table] = $hash;
// $FK = $relation->getQualifiedForeignPivotKeyName();

// // pivot

// $this->groupBy($relation->getQualifiedParentKeyName());
// $PK = $relation->getQualifiedParentKeyName();
// $query->from($table);

// if(empty($this->getQuery()->orders)) {
// $this->orderBy($relation->getQualifiedParentKeyName(), "asc"); // apply default order
// }
// // handle self relations
// // if(get_class($relation->getParent()) === get_class($relation->getRelated())) {
// $query->from($table.' as '.$hash);
// $related->setTable($hash);
// $FK = str_replace($table, $hash, $FK);
// // }

// break;
default:
throw new \Exception("joinsToModel can only be called on belongsTo and hasOne relations.");
}

$this->leftJoin($query->from, $PK, '=', $FK);
$this->priorJoinToModelParents[$table] = $hash;

// dd($this->toSql());
return $this;
};
}
Expand Down
Loading

0 comments on commit 43e9035

Please sign in to comment.