|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Database\Eloquent\Relations\Concerns; |
| 6 | + |
| 7 | +use Hyperf\Collection\Collection; |
| 8 | +use Hyperf\Database\Model\Relations\Pivot; |
| 9 | + |
| 10 | +/** |
| 11 | + * Overrides Hyperf's InteractsWithPivotTable to support pivot model events. |
| 12 | + * |
| 13 | + * When a custom pivot class is specified via `->using()`, operations like |
| 14 | + * attach/detach/update use model methods (save/delete) instead of raw queries, |
| 15 | + * enabling model events (creating, created, deleting, deleted, etc.) to fire. |
| 16 | + * |
| 17 | + * Without `->using()`, the parent's performant bulk query behavior is preserved. |
| 18 | + */ |
| 19 | +trait InteractsWithPivotTable |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Attach a model to the parent. |
| 23 | + * |
| 24 | + * @param mixed $id |
| 25 | + * @param bool $touch |
| 26 | + */ |
| 27 | + public function attach($id, array $attributes = [], $touch = true) |
| 28 | + { |
| 29 | + if ($this->using) { |
| 30 | + $this->attachUsingCustomClass($id, $attributes); |
| 31 | + } else { |
| 32 | + parent::attach($id, $attributes, $touch); |
| 33 | + |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if ($touch) { |
| 38 | + $this->touchIfTouching(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Attach a model to the parent using a custom class. |
| 44 | + * |
| 45 | + * @param mixed $ids |
| 46 | + */ |
| 47 | + protected function attachUsingCustomClass($ids, array $attributes) |
| 48 | + { |
| 49 | + $records = $this->formatAttachRecords( |
| 50 | + $this->parseIds($ids), |
| 51 | + $attributes |
| 52 | + ); |
| 53 | + |
| 54 | + foreach ($records as $record) { |
| 55 | + $this->newPivot($record, false)->save(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Detach models from the relationship. |
| 61 | + * |
| 62 | + * @param mixed $ids |
| 63 | + * @param bool $touch |
| 64 | + */ |
| 65 | + public function detach($ids = null, $touch = true) |
| 66 | + { |
| 67 | + if ($this->using) { |
| 68 | + $results = $this->detachUsingCustomClass($ids); |
| 69 | + } else { |
| 70 | + return parent::detach($ids, $touch); |
| 71 | + } |
| 72 | + |
| 73 | + if ($touch) { |
| 74 | + $this->touchIfTouching(); |
| 75 | + } |
| 76 | + |
| 77 | + return $results; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Detach models from the relationship using a custom class. |
| 82 | + * |
| 83 | + * @param mixed $ids |
| 84 | + * @return int |
| 85 | + */ |
| 86 | + protected function detachUsingCustomClass($ids) |
| 87 | + { |
| 88 | + $results = 0; |
| 89 | + |
| 90 | + $pivots = $this->getCurrentlyAttachedPivots($ids); |
| 91 | + |
| 92 | + foreach ($pivots as $pivot) { |
| 93 | + $results += $pivot->delete(); |
| 94 | + } |
| 95 | + |
| 96 | + return $results; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Update an existing pivot record on the table. |
| 101 | + * |
| 102 | + * @param mixed $id |
| 103 | + * @param bool $touch |
| 104 | + */ |
| 105 | + public function updateExistingPivot($id, array $attributes, $touch = true) |
| 106 | + { |
| 107 | + if ($this->using) { |
| 108 | + return $this->updateExistingPivotUsingCustomClass($id, $attributes, $touch); |
| 109 | + } |
| 110 | + |
| 111 | + return parent::updateExistingPivot($id, $attributes, $touch); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Update an existing pivot record on the table via a custom class. |
| 116 | + * |
| 117 | + * @param mixed $id |
| 118 | + * @return int |
| 119 | + */ |
| 120 | + protected function updateExistingPivotUsingCustomClass($id, array $attributes, bool $touch) |
| 121 | + { |
| 122 | + $pivot = $this->getCurrentlyAttachedPivots($id)->first(); |
| 123 | + |
| 124 | + $updated = $pivot ? $pivot->fill($attributes)->isDirty() : false; |
| 125 | + |
| 126 | + if ($updated) { |
| 127 | + $pivot->save(); |
| 128 | + } |
| 129 | + |
| 130 | + if ($touch) { |
| 131 | + $this->touchIfTouching(); |
| 132 | + } |
| 133 | + |
| 134 | + return (int) $updated; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get the pivot models that are currently attached. |
| 139 | + * |
| 140 | + * @param mixed $ids |
| 141 | + */ |
| 142 | + protected function getCurrentlyAttachedPivots($ids = null): Collection |
| 143 | + { |
| 144 | + $query = $this->newPivotQuery(); |
| 145 | + |
| 146 | + if ($ids !== null) { |
| 147 | + $query->whereIn($this->relatedPivotKey, $this->parseIds($ids)); |
| 148 | + } |
| 149 | + |
| 150 | + return $query->get()->map(function ($record) { |
| 151 | + /** @var class-string<Pivot> $class */ |
| 152 | + $class = $this->using ?: Pivot::class; |
| 153 | + |
| 154 | + return $class::fromRawAttributes($this->parent, (array) $record, $this->getTable(), true) |
| 155 | + ->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Create a new pivot model instance. |
| 161 | + * |
| 162 | + * Overrides parent to include pivotValues in the attributes. |
| 163 | + * |
| 164 | + * @param bool $exists |
| 165 | + */ |
| 166 | + public function newPivot(array $attributes = [], $exists = false) |
| 167 | + { |
| 168 | + $attributes = array_merge( |
| 169 | + array_column($this->pivotValues, 'value', 'column'), |
| 170 | + $attributes |
| 171 | + ); |
| 172 | + |
| 173 | + /** @var Pivot $pivot */ |
| 174 | + $pivot = $this->related->newPivot( |
| 175 | + $this->parent, |
| 176 | + $attributes, |
| 177 | + $this->table, |
| 178 | + $exists, |
| 179 | + $this->using |
| 180 | + ); |
| 181 | + |
| 182 | + return $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); |
| 183 | + } |
| 184 | +} |
0 commit comments