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)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
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); |
||||
0 ignored issues
–
show
The method
applyIds() does not exist on MaksimM\CompositePrimary...ompositeKeyQueryBuilder . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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, |
||||
92 | $name |
||||
93 | ); |
||||
94 | |||||
95 | $this->enableBinaryMutators($models); |
||||
96 | $this->enableBinaryMutators($eagerRelation); |
||||
97 | |||||
98 | return $matchedRelations; |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * OneTwoMany and Similar Relation support for binary columns. |
||||
103 | * |
||||
104 | * @param $models |
||||
105 | */ |
||||
106 | private function disableBinaryMutators($models) |
||||
107 | { |
||||
108 | foreach ($models as $model) { |
||||
109 | if (method_exists($model, 'disableBinaryMutators')) { |
||||
110 | $model->disableBinaryMutators(); |
||||
111 | } |
||||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * OneTwoMany and Similar Relation support for binary columns. |
||||
117 | * |
||||
118 | * @param $models |
||||
119 | */ |
||||
120 | private function enableBinaryMutators($models) |
||||
121 | { |
||||
122 | foreach ($models as $model) { |
||||
123 | if (method_exists($model, 'enableBinaryMutators')) { |
||||
124 | $model->enableBinaryMutators(); |
||||
125 | } |
||||
126 | } |
||||
127 | } |
||||
128 | } |
||||
129 |