|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
use Illuminate\Support\Arr; |
|
11
|
|
|
use MaksimM\CompositePrimaryKeys\Exceptions\WrongKeyException; |
|
12
|
|
|
|
|
13
|
|
|
class CompositeKeyQueryBuilder extends Builder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Find a model by its primary key. |
|
17
|
|
|
* |
|
18
|
|
|
* @param mixed $id |
|
19
|
|
|
* @param array $columns |
|
20
|
|
|
* |
|
21
|
|
|
*@throws WrongKeyException |
|
22
|
|
|
* |
|
23
|
|
|
* @return Model|Collection|static[]|static|null |
|
24
|
|
|
*/ |
|
25
|
|
|
public function find($id, $columns = ['*']) |
|
26
|
|
|
{ |
|
27
|
|
|
if ((!$this->getModel()->hasCompositeIndex() || (is_array($id) && !Arr::isAssoc($id))) && (is_array($id) || $id instanceof Arrayable)) { |
|
|
|
|
|
|
28
|
|
|
return $this->findMany($id, $columns); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this->whereKey($this->getModel()->hasCompositeIndex() ? [$id] : $id)->first($columns); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Add a where clause on the primary key to the query. |
|
36
|
|
|
* |
|
37
|
|
|
* @param mixed $ids |
|
38
|
|
|
* |
|
39
|
|
|
* @throws WrongKeyException |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function whereKey($ids) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->applyIds($ids); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a where clause on the primary key to the query. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $ids |
|
52
|
|
|
* |
|
53
|
|
|
* @throws WrongKeyException |
|
54
|
|
|
* |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function whereKeyNot($ids) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->applyIds($ids, true); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Eagerly load the relationship on a set of models. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $models |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @param Closure $constraints |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function eagerLoadRelation(array $models, $name, Closure $constraints) |
|
72
|
|
|
{ |
|
73
|
|
|
// First we will "back up" the existing where conditions on the query so we can |
|
74
|
|
|
// add our eager constraints. Then we will merge the wheres that were on the |
|
75
|
|
|
// query back to it in order that any where conditions might be specified. |
|
76
|
|
|
$relation = $this->getRelation($name); |
|
77
|
|
|
|
|
78
|
|
|
$this->disableBinaryMutators($models); |
|
79
|
|
|
$relation->addEagerConstraints($models); |
|
80
|
|
|
|
|
81
|
|
|
$constraints($relation); |
|
82
|
|
|
|
|
83
|
|
|
// Once we have the results, we just match those back up to their parent models |
|
84
|
|
|
// using the relationship instance. Then we just return the finished arrays |
|
85
|
|
|
// of models which have been eagerly hydrated and are readied for return. |
|
86
|
|
|
$eagerRelation = $relation->getEager(); |
|
87
|
|
|
$this->disableBinaryMutators($eagerRelation); |
|
88
|
|
|
|
|
89
|
|
|
$matchedRelations = $relation->match( |
|
90
|
|
|
$relation->initRelation($models, $name), |
|
91
|
|
|
$eagerRelation, $name |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$this->enableBinaryMutators($models); |
|
95
|
|
|
$this->enableBinaryMutators($eagerRelation); |
|
96
|
|
|
|
|
97
|
|
|
return $matchedRelations; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* OneTwoMany and Similar Relation support for binary columns. |
|
102
|
|
|
* |
|
103
|
|
|
* @param $models |
|
104
|
|
|
*/ |
|
105
|
|
|
private function disableBinaryMutators($models) |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($models as $model) { |
|
108
|
|
|
if (method_exists($model, 'disableBinaryMutators')) { |
|
109
|
|
|
$model->disableBinaryMutators(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* OneTwoMany and Similar Relation support for binary columns. |
|
116
|
|
|
* |
|
117
|
|
|
* @param $models |
|
118
|
|
|
*/ |
|
119
|
|
|
private function enableBinaryMutators($models) |
|
120
|
|
|
{ |
|
121
|
|
|
foreach ($models as $model) { |
|
122
|
|
|
if (method_exists($model, 'enableBinaryMutators')) { |
|
123
|
|
|
$model->enableBinaryMutators(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|