-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support HasManyThrough relationships on PostgreSQL
- Loading branch information
1 parent
2d9dd76
commit fef0069
Showing
11 changed files
with
242 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Staudenmeir\EloquentJsonRelations\HasManyThrough as Base; | ||
|
||
class HasManyThrough extends Base | ||
{ | ||
use IsPostgresRelation; | ||
|
||
/** | ||
* Set the join clause on the query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder|null $query | ||
* @return void | ||
*/ | ||
protected function performJoin(Builder $query = null) | ||
{ | ||
$query = $query ?: $this->query; | ||
|
||
$farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey); | ||
|
||
$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey); | ||
|
||
if ($this->throughParentSoftDeletes()) { | ||
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); | ||
} | ||
} | ||
|
||
/** | ||
* Add the constraints for a relationship query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param \Illuminate\Database\Eloquent\Builder $parentQuery | ||
* @param array|mixed $columns | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) | ||
{ | ||
if ($parentQuery->getQuery()->from === $query->getQuery()->from) { | ||
return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); | ||
} | ||
|
||
if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) { | ||
return $this->getRelationExistenceQueryForThroughSelfRelation($query, $parentQuery, $columns); | ||
} | ||
|
||
$this->performJoin($query); | ||
|
||
$firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey); | ||
|
||
return $query->select($columns)->whereColumn( | ||
$this->getQualifiedLocalKeyName(), '=', $firstKey | ||
); | ||
} | ||
|
||
/** | ||
* Add the constraints for a relationship query on the same table. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param \Illuminate\Database\Eloquent\Builder $parentQuery | ||
* @param array|mixed $columns | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) | ||
{ | ||
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); | ||
|
||
$farKey = $this->jsonColumn($query, $this->throughParent, $hash.'.'.$this->secondKey, $this->secondLocalKey); | ||
|
||
$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey); | ||
|
||
$query->getModel()->setTable($hash); | ||
|
||
$firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey); | ||
|
||
return $query->select($columns)->whereColumn( | ||
$parentQuery->getQuery()->from.'.'.$this->localKey, '=', $firstKey | ||
); | ||
} | ||
|
||
/** | ||
* Add the constraints for a relationship query on the same table as the through parent. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param \Illuminate\Database\Eloquent\Builder $parentQuery | ||
* @param array|mixed $columns | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function getRelationExistenceQueryForThroughSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) | ||
{ | ||
$table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash(); | ||
|
||
$farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey); | ||
|
||
$query->join($table, $hash.'.'.$this->secondLocalKey, '=', $farKey); | ||
|
||
if ($this->throughParentSoftDeletes()) { | ||
$query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn()); | ||
} | ||
|
||
$firstKey = $this->jsonColumn($query, $this->farParent, $hash.'.'.$this->firstKey, $this->localKey); | ||
|
||
return $query->select($columns)->whereColumn( | ||
$parentQuery->getQuery()->from.'.'.$this->localKey, '=', $firstKey | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class Category extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
protected $casts = [ | ||
'options' => 'json' | ||
]; | ||
|
||
public function subProducts() | ||
{ | ||
return $this->hasManyThrough(Product::class, self::class, 'options->parent_id', 'options->category_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tests\Models; | ||
|
||
class Product extends Model | ||
{ | ||
protected $casts = [ | ||
'options' => 'json' | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tests\Models; | ||
|
||
class Team extends Model | ||
{ | ||
protected $casts = [ | ||
'options' => 'json' | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters