1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentEagerLimit; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Staudenmeir\EloquentEagerLimit\Grammars\MySqlGrammar; |
10
|
|
|
use Staudenmeir\EloquentEagerLimit\Grammars\PostgresGrammar; |
11
|
|
|
use Staudenmeir\EloquentEagerLimit\Grammars\SQLiteGrammar; |
12
|
|
|
use Staudenmeir\EloquentEagerLimit\Grammars\SqlServerGrammar; |
13
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\BelongsToMany; |
14
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\HasMany; |
15
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\HasManyThrough; |
16
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\HasOne; |
17
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\HasOneThrough; |
18
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\MorphMany; |
19
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\MorphOne; |
20
|
|
|
use Staudenmeir\EloquentEagerLimit\Relations\MorphToMany; |
21
|
|
|
|
22
|
|
|
trait HasEagerLimit |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Get a new query builder instance for the connection. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Database\Query\Builder |
28
|
|
|
*/ |
29
|
148 |
|
protected function newBaseQueryBuilder() |
30
|
|
|
{ |
31
|
148 |
|
$connection = $this->getConnection(); |
|
|
|
|
32
|
|
|
|
33
|
148 |
|
$grammar = $connection->withTablePrefix($this->getQueryGrammar($connection)); |
34
|
|
|
|
35
|
148 |
|
return new \Staudenmeir\EloquentEagerLimit\Builder( |
36
|
148 |
|
$connection, |
37
|
|
|
$grammar, |
38
|
148 |
|
$connection->getPostProcessor() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the query grammar. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Database\Connection $connection |
46
|
|
|
* @return \Illuminate\Database\Query\Grammars\Grammar |
47
|
|
|
*/ |
48
|
148 |
|
protected function getQueryGrammar(Connection $connection) |
49
|
|
|
{ |
50
|
148 |
|
$driver = $connection->getDriverName(); |
51
|
|
|
|
52
|
148 |
|
switch ($driver) { |
53
|
148 |
|
case 'mysql': |
54
|
37 |
|
return new MySqlGrammar(); |
55
|
111 |
|
case 'pgsql': |
56
|
37 |
|
return new PostgresGrammar(); |
57
|
74 |
|
case 'sqlite': |
58
|
37 |
|
return new SQLiteGrammar(); |
59
|
37 |
|
case 'sqlsrv': |
60
|
37 |
|
return new SqlServerGrammar(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Instantiate a new HasOne relationship. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
70
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
71
|
|
|
* @param string $foreignKey |
72
|
|
|
* @param string $localKey |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
74
|
|
|
*/ |
75
|
24 |
|
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) |
76
|
|
|
{ |
77
|
24 |
|
return new HasOne($query, $parent, $foreignKey, $localKey); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Instantiate a new HasOneThrough relationship. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
84
|
|
|
* @param \Illuminate\Database\Eloquent\Model $farParent |
85
|
|
|
* @param \Illuminate\Database\Eloquent\Model $throughParent |
86
|
|
|
* @param string $firstKey |
87
|
|
|
* @param string $secondKey |
88
|
|
|
* @param string $localKey |
89
|
|
|
* @param string $secondLocalKey |
90
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough |
91
|
|
|
*/ |
92
|
16 |
|
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
93
|
|
|
{ |
94
|
16 |
|
return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Instantiate a new MorphOne relationship. |
99
|
|
|
* |
100
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
101
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
102
|
|
|
* @param string $type |
103
|
|
|
* @param string $id |
104
|
|
|
* @param string $localKey |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
106
|
|
|
*/ |
107
|
16 |
|
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) |
108
|
|
|
{ |
109
|
16 |
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Instantiate a new HasMany relationship. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
116
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
117
|
|
|
* @param string $foreignKey |
118
|
|
|
* @param string $localKey |
119
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
120
|
|
|
*/ |
121
|
16 |
|
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) |
122
|
|
|
{ |
123
|
16 |
|
return new HasMany($query, $parent, $foreignKey, $localKey); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Instantiate a new HasManyThrough relationship. |
128
|
|
|
* |
129
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
130
|
|
|
* @param \Illuminate\Database\Eloquent\Model $farParent |
131
|
|
|
* @param \Illuminate\Database\Eloquent\Model $throughParent |
132
|
|
|
* @param string $firstKey |
133
|
|
|
* @param string $secondKey |
134
|
|
|
* @param string $localKey |
135
|
|
|
* @param string $secondLocalKey |
136
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
137
|
|
|
*/ |
138
|
16 |
|
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
139
|
|
|
{ |
140
|
16 |
|
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Instantiate a new MorphMany relationship. |
145
|
|
|
* |
146
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
147
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
148
|
|
|
* @param string $type |
149
|
|
|
* @param string $id |
150
|
|
|
* @param string $localKey |
151
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
152
|
|
|
*/ |
153
|
16 |
|
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) |
154
|
|
|
{ |
155
|
16 |
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Instantiate a new BelongsToMany relationship. |
160
|
|
|
* |
161
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
162
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
163
|
|
|
* @param string $table |
164
|
|
|
* @param string $foreignPivotKey |
165
|
|
|
* @param string $relatedPivotKey |
166
|
|
|
* @param string $parentKey |
167
|
|
|
* @param string $relatedKey |
168
|
|
|
* @param string $relationName |
169
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
170
|
|
|
*/ |
171
|
16 |
|
protected function newBelongsToMany( |
172
|
|
|
Builder $query, |
173
|
|
|
Model $parent, |
174
|
|
|
$table, |
175
|
|
|
$foreignPivotKey, |
176
|
|
|
$relatedPivotKey, |
177
|
|
|
$parentKey, |
178
|
|
|
$relatedKey, |
179
|
|
|
$relationName = null |
180
|
|
|
) |
181
|
|
|
{ |
182
|
16 |
|
return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Instantiate a new MorphToMany relationship. |
187
|
|
|
* |
188
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
189
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
190
|
|
|
* @param string $name |
191
|
|
|
* @param string $table |
192
|
|
|
* @param string $foreignPivotKey |
193
|
|
|
* @param string $relatedPivotKey |
194
|
|
|
* @param string $parentKey |
195
|
|
|
* @param string $relatedKey |
196
|
|
|
* @param string $relationName |
197
|
|
|
* @param bool $inverse |
198
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
199
|
|
|
*/ |
200
|
16 |
|
protected function newMorphToMany( |
201
|
|
|
Builder $query, |
202
|
|
|
Model $parent, |
203
|
|
|
$name, |
204
|
|
|
$table, |
205
|
|
|
$foreignPivotKey, |
206
|
|
|
$relatedPivotKey, |
207
|
|
|
$parentKey, |
208
|
|
|
$relatedKey, |
209
|
|
|
$relationName = null, |
210
|
|
|
$inverse = false |
211
|
|
|
) |
212
|
|
|
{ |
213
|
16 |
|
return new MorphToMany( |
214
|
16 |
|
$query, |
215
|
|
|
$parent, |
216
|
|
|
$name, |
217
|
|
|
$table, |
218
|
|
|
$foreignPivotKey, |
219
|
|
|
$relatedPivotKey, |
220
|
|
|
$parentKey, |
221
|
|
|
$relatedKey, |
222
|
|
|
$relationName, |
223
|
|
|
$inverse |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: