1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blasttech\EloquentRelatedPlus; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
10
|
|
|
|
11
|
|
|
trait HelperMethodTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get the join columns for a relation |
16
|
|
|
* |
17
|
|
|
* @param Relation|BelongsTo|HasOneOrMany $relation |
18
|
|
|
* @return \stdClass |
19
|
|
|
*/ |
20
|
|
|
private function getJoinColumns($relation) |
21
|
|
|
{ |
22
|
|
|
// Get keys with table names |
23
|
|
|
if ($relation instanceof BelongsTo) { |
24
|
|
|
$first = $relation->getOwnerKey(); |
25
|
|
|
$second = $relation->getForeignKey(); |
26
|
|
|
} else { |
27
|
|
|
$first = $relation->getQualifiedParentKeyName(); |
28
|
|
|
$second = $relation->getQualifiedForeignKeyName(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return (object)['first' => $first, 'second' => $second]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the relations from a relation name |
36
|
|
|
* $relationName can be a single relation |
37
|
|
|
* Usage for User model: |
38
|
|
|
* parseRelationNames('customer') returns [$user->customer()] |
39
|
|
|
* parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
40
|
|
|
* |
41
|
|
|
* @param string $relationName |
42
|
|
|
* @return Relation[] |
43
|
|
|
*/ |
44
|
|
|
private function parseRelationNames($relationName) |
45
|
|
|
{ |
46
|
|
|
$relationNames = explode('.', $relationName); |
47
|
|
|
$parentRelationName = null; |
48
|
|
|
$relations = []; |
49
|
|
|
|
50
|
|
|
foreach ($relationNames as $relationName) { |
51
|
|
|
if (is_null($parentRelationName)) { |
52
|
|
|
$relations[] = $this->$relationName(); |
53
|
|
|
$parentRelationName = $this->$relationName()->getRelated(); |
54
|
|
|
} else { |
55
|
|
|
$relations[] = $parentRelationName->$relationName(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $relations; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add backticks to a table/column |
64
|
|
|
* |
65
|
|
|
* @param string $column |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
private function addBackticks($column) |
69
|
|
|
{ |
70
|
|
|
return preg_match('/^[0-9a-zA-Z\.]*$/', $column) ? |
71
|
|
|
'`' . str_replace(['`', '.'], ['', '`.`'], $column) . '`' : $column; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the sql for a query with the bindings replaced with the binding values |
76
|
|
|
* |
77
|
|
|
* @param Builder $builder |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function toSqlWithBindings(Builder $builder) |
81
|
|
|
{ |
82
|
|
|
$replacements = array_map('addslashes', $builder->getBindings()); |
83
|
|
|
$sql = $builder->toSql(); |
84
|
|
|
|
85
|
|
|
return preg_replace_callback( |
86
|
|
|
'/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/', |
87
|
|
|
function () use (&$replacements) { |
88
|
|
|
return array_shift($replacements); |
89
|
|
|
}, |
90
|
|
|
$sql |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add table name to column name if table name not already included in column name |
96
|
|
|
* |
97
|
|
|
* @param string $table |
98
|
|
|
* @param string $column |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function columnWithTableName($table, $column) |
102
|
|
|
{ |
103
|
|
|
return (preg_match('/(' . $table . '\.|`' . $table . '`)/i', $column) > 0 ? '' : $table . '.') . $column; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove a global scope if it exists |
108
|
|
|
* |
109
|
|
|
* @param Builder $query |
110
|
|
|
* @param string $scopeName |
111
|
|
|
* @return Builder |
112
|
|
|
*/ |
113
|
|
|
private function removeGlobalScope($query, $scopeName) |
114
|
|
|
{ |
115
|
|
|
/** @var Model $this */ |
116
|
|
|
$globalScopes = $this->getGlobalScopes(); |
117
|
|
|
if (isset($globalScopes[$scopeName])) { |
118
|
|
|
$query->withoutGlobalScope($scopeName); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $query; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check if this model has already been joined to a table or relation |
126
|
|
|
* |
127
|
|
|
* @param Builder $builder |
128
|
|
|
* @param string $table |
129
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
private function hasJoin(Builder $builder, $table, $relation) |
133
|
|
|
{ |
134
|
|
|
if (!$this->isJoined($builder, $table)) { |
135
|
|
|
return $this->isEagerLoaded($builder, $relation); |
136
|
|
|
} else { |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check if model is currently joined to $table |
143
|
|
|
* |
144
|
|
|
* @param Builder $builder |
145
|
|
|
* @param string $table |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
private function isJoined(Builder $builder, $table) |
149
|
|
|
{ |
150
|
|
|
$joins = $builder->getQuery()->joins; |
151
|
|
|
if (!is_null($joins)) { |
152
|
|
|
foreach ($joins as $joinClause) { |
153
|
|
|
if ($joinClause->table == $table) { |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check if relation exists in eager loads |
164
|
|
|
* |
165
|
|
|
* @param Builder $builder |
166
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
private function isEagerLoaded(Builder $builder, $relation) |
170
|
|
|
{ |
171
|
|
|
$eagerLoads = $builder->getEagerLoads(); |
172
|
|
|
|
173
|
|
|
return !is_null($eagerLoads) && in_array($relation, $eagerLoads); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|