|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
|
|
7
|
|
|
trait HasOneOrManyThrough |
|
8
|
|
|
{ |
|
9
|
|
|
use IsPostgresRelation; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Set the join clause on the query. |
|
13
|
|
|
* |
|
14
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|null $query |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function performJoin(?Builder $query = null) |
|
18
|
|
|
{ |
|
19
|
|
|
$query = $query ?: $this->query; |
|
20
|
|
|
|
|
21
|
|
|
$farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey); |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if ($this->throughParentSoftDeletes()) { |
|
|
|
|
|
|
26
|
|
|
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add the constraints for a relationship query. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
34
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
|
35
|
|
|
* @param array|mixed $columns |
|
36
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($parentQuery->getQuery()->from === $query->getQuery()->from) { |
|
41
|
|
|
return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) { |
|
45
|
|
|
return $this->getRelationExistenceQueryForThroughSelfRelation($query, $parentQuery, $columns); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->performJoin($query); |
|
49
|
|
|
|
|
50
|
|
|
$firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
|
|
53
|
|
|
$this->getQualifiedLocalKeyName(), |
|
|
|
|
|
|
54
|
|
|
'=', |
|
55
|
|
|
$firstKey |
|
|
|
|
|
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add the constraints for a relationship query on the same table. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
63
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
|
64
|
|
|
* @param array|mixed $columns |
|
65
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
|
68
|
|
|
{ |
|
69
|
|
|
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$farKey = $this->jsonColumn($query, $this->throughParent, $hash.'.'.$this->secondKey, $this->secondLocalKey); |
|
72
|
|
|
|
|
73
|
|
|
$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey); |
|
74
|
|
|
|
|
75
|
|
|
$query->getModel()->setTable($hash); |
|
76
|
|
|
|
|
77
|
|
|
$firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey); |
|
78
|
|
|
|
|
79
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
|
|
80
|
|
|
$parentQuery->getQuery()->from.'.'.$this->localKey, |
|
|
|
|
|
|
81
|
|
|
'=', |
|
82
|
|
|
$firstKey |
|
|
|
|
|
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Add the constraints for a relationship query on the same table as the through parent. |
|
88
|
|
|
* |
|
89
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
90
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
|
91
|
|
|
* @param array|mixed $columns |
|
92
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getRelationExistenceQueryForThroughSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
|
95
|
|
|
{ |
|
96
|
|
|
$table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash(); |
|
97
|
|
|
|
|
98
|
|
|
$farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey); |
|
99
|
|
|
|
|
100
|
|
|
$query->join($table, $hash.'.'.$this->secondLocalKey, '=', $farKey); |
|
101
|
|
|
|
|
102
|
|
|
if ($this->throughParentSoftDeletes()) { |
|
103
|
|
|
$query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$firstKey = $this->jsonColumn($query, $this->farParent, $hash.'.'.$this->firstKey, $this->localKey); |
|
107
|
|
|
|
|
108
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
|
|
109
|
|
|
$parentQuery->getQuery()->from.'.'.$this->localKey, |
|
|
|
|
|
|
110
|
|
|
'=', |
|
111
|
|
|
$firstKey |
|
|
|
|
|
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|