independent-analytics
/
vendor
/
illuminate
/
database
/
Eloquent
/
Relations
/
Concerns
/
AsPivot.php
independent-analytics
/
vendor
/
illuminate
/
database
/
Eloquent
/
Relations
/
Concerns
Last commit date
AsPivot.php
1 month ago
CanBeOneOfMany.php
1 month ago
ComparesRelatedModels.php
2 years ago
InteractsWithDictionary.php
1 month ago
InteractsWithPivotTable.php
1 month ago
SupportsDefaultModels.php
2 years ago
AsPivot.php
262 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent\Relations\Concerns; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Eloquent\Model; |
| 6 | use IAWPSCOPED\Illuminate\Support\Str; |
| 7 | /** @internal */ |
| 8 | trait AsPivot |
| 9 | { |
| 10 | /** |
| 11 | * The parent model of the relationship. |
| 12 | * |
| 13 | * @var \Illuminate\Database\Eloquent\Model |
| 14 | */ |
| 15 | public $pivotParent; |
| 16 | /** |
| 17 | * The name of the foreign key column. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $foreignKey; |
| 22 | /** |
| 23 | * The name of the "other key" column. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $relatedKey; |
| 28 | /** |
| 29 | * Create a new pivot model instance. |
| 30 | * |
| 31 | * @param \Illuminate\Database\Eloquent\Model $parent |
| 32 | * @param array $attributes |
| 33 | * @param string $table |
| 34 | * @param bool $exists |
| 35 | * @return static |
| 36 | */ |
| 37 | public static function fromAttributes(Model $parent, $attributes, $table, $exists = \false) |
| 38 | { |
| 39 | $instance = new static(); |
| 40 | $instance->timestamps = $instance->hasTimestampAttributes($attributes); |
| 41 | // The pivot model is a "dynamic" model since we will set the tables dynamically |
| 42 | // for the instance. This allows it work for any intermediate tables for the |
| 43 | // many to many relationship that are defined by this developer's classes. |
| 44 | $instance->setConnection($parent->getConnectionName())->setTable($table)->forceFill($attributes)->syncOriginal(); |
| 45 | // We store off the parent instance so we will access the timestamp column names |
| 46 | // for the model, since the pivot model timestamps aren't easily configurable |
| 47 | // from the developer's point of view. We can use the parents to get these. |
| 48 | $instance->pivotParent = $parent; |
| 49 | $instance->exists = $exists; |
| 50 | return $instance; |
| 51 | } |
| 52 | /** |
| 53 | * Create a new pivot model from raw values returned from a query. |
| 54 | * |
| 55 | * @param \Illuminate\Database\Eloquent\Model $parent |
| 56 | * @param array $attributes |
| 57 | * @param string $table |
| 58 | * @param bool $exists |
| 59 | * @return static |
| 60 | */ |
| 61 | public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = \false) |
| 62 | { |
| 63 | $instance = static::fromAttributes($parent, [], $table, $exists); |
| 64 | $instance->timestamps = $instance->hasTimestampAttributes($attributes); |
| 65 | $instance->setRawAttributes(\array_merge($instance->getRawOriginal(), $attributes), $exists); |
| 66 | return $instance; |
| 67 | } |
| 68 | /** |
| 69 | * Set the keys for a select query. |
| 70 | * |
| 71 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 72 | * @return \Illuminate\Database\Eloquent\Builder |
| 73 | */ |
| 74 | protected function setKeysForSelectQuery($query) |
| 75 | { |
| 76 | if (isset($this->attributes[$this->getKeyName()])) { |
| 77 | return parent::setKeysForSelectQuery($query); |
| 78 | } |
| 79 | $query->where($this->foreignKey, $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey))); |
| 80 | return $query->where($this->relatedKey, $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey))); |
| 81 | } |
| 82 | /** |
| 83 | * Set the keys for a save update query. |
| 84 | * |
| 85 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 86 | * @return \Illuminate\Database\Eloquent\Builder |
| 87 | */ |
| 88 | protected function setKeysForSaveQuery($query) |
| 89 | { |
| 90 | return $this->setKeysForSelectQuery($query); |
| 91 | } |
| 92 | /** |
| 93 | * Delete the pivot model record from the database. |
| 94 | * |
| 95 | * @return int |
| 96 | */ |
| 97 | public function delete() |
| 98 | { |
| 99 | if (isset($this->attributes[$this->getKeyName()])) { |
| 100 | return (int) parent::delete(); |
| 101 | } |
| 102 | if ($this->fireModelEvent('deleting') === \false) { |
| 103 | return 0; |
| 104 | } |
| 105 | $this->touchOwners(); |
| 106 | return \IAWPSCOPED\tap($this->getDeleteQuery()->delete(), function () { |
| 107 | $this->exists = \false; |
| 108 | $this->fireModelEvent('deleted', \false); |
| 109 | }); |
| 110 | } |
| 111 | /** |
| 112 | * Get the query builder for a delete operation on the pivot. |
| 113 | * |
| 114 | * @return \Illuminate\Database\Eloquent\Builder |
| 115 | */ |
| 116 | protected function getDeleteQuery() |
| 117 | { |
| 118 | return $this->newQueryWithoutRelationships()->where([$this->foreignKey => $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey)), $this->relatedKey => $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey))]); |
| 119 | } |
| 120 | /** |
| 121 | * Get the table associated with the model. |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public function getTable() |
| 126 | { |
| 127 | if (!isset($this->table)) { |
| 128 | $this->setTable(\str_replace('\\', '', Str::snake(Str::singular(class_basename($this))))); |
| 129 | } |
| 130 | return $this->table; |
| 131 | } |
| 132 | /** |
| 133 | * Get the foreign key column name. |
| 134 | * |
| 135 | * @return string |
| 136 | */ |
| 137 | public function getForeignKey() |
| 138 | { |
| 139 | return $this->foreignKey; |
| 140 | } |
| 141 | /** |
| 142 | * Get the "related key" column name. |
| 143 | * |
| 144 | * @return string |
| 145 | */ |
| 146 | public function getRelatedKey() |
| 147 | { |
| 148 | return $this->relatedKey; |
| 149 | } |
| 150 | /** |
| 151 | * Get the "related key" column name. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function getOtherKey() |
| 156 | { |
| 157 | return $this->getRelatedKey(); |
| 158 | } |
| 159 | /** |
| 160 | * Set the key names for the pivot model instance. |
| 161 | * |
| 162 | * @param string $foreignKey |
| 163 | * @param string $relatedKey |
| 164 | * @return $this |
| 165 | */ |
| 166 | public function setPivotKeys($foreignKey, $relatedKey) |
| 167 | { |
| 168 | $this->foreignKey = $foreignKey; |
| 169 | $this->relatedKey = $relatedKey; |
| 170 | return $this; |
| 171 | } |
| 172 | /** |
| 173 | * Determine if the pivot model or given attributes has timestamp attributes. |
| 174 | * |
| 175 | * @param array|null $attributes |
| 176 | * @return bool |
| 177 | */ |
| 178 | public function hasTimestampAttributes($attributes = null) |
| 179 | { |
| 180 | return \array_key_exists($this->getCreatedAtColumn(), $attributes ?? $this->attributes); |
| 181 | } |
| 182 | /** |
| 183 | * Get the name of the "created at" column. |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | public function getCreatedAtColumn() |
| 188 | { |
| 189 | return $this->pivotParent ? $this->pivotParent->getCreatedAtColumn() : parent::getCreatedAtColumn(); |
| 190 | } |
| 191 | /** |
| 192 | * Get the name of the "updated at" column. |
| 193 | * |
| 194 | * @return string |
| 195 | */ |
| 196 | public function getUpdatedAtColumn() |
| 197 | { |
| 198 | return $this->pivotParent ? $this->pivotParent->getUpdatedAtColumn() : parent::getUpdatedAtColumn(); |
| 199 | } |
| 200 | /** |
| 201 | * Get the queueable identity for the entity. |
| 202 | * |
| 203 | * @return mixed |
| 204 | */ |
| 205 | public function getQueueableId() |
| 206 | { |
| 207 | if (isset($this->attributes[$this->getKeyName()])) { |
| 208 | return $this->getKey(); |
| 209 | } |
| 210 | return \sprintf('%s:%s:%s:%s', $this->foreignKey, $this->getAttribute($this->foreignKey), $this->relatedKey, $this->getAttribute($this->relatedKey)); |
| 211 | } |
| 212 | /** |
| 213 | * Get a new query to restore one or more models by their queueable IDs. |
| 214 | * |
| 215 | * @param int[]|string[]|string $ids |
| 216 | * @return \Illuminate\Database\Eloquent\Builder |
| 217 | */ |
| 218 | public function newQueryForRestoration($ids) |
| 219 | { |
| 220 | if (\is_array($ids)) { |
| 221 | return $this->newQueryForCollectionRestoration($ids); |
| 222 | } |
| 223 | if (!\str_contains($ids, ':')) { |
| 224 | return parent::newQueryForRestoration($ids); |
| 225 | } |
| 226 | $segments = \explode(':', $ids); |
| 227 | return $this->newQueryWithoutScopes()->where($segments[0], $segments[1])->where($segments[2], $segments[3]); |
| 228 | } |
| 229 | /** |
| 230 | * Get a new query to restore multiple models by their queueable IDs. |
| 231 | * |
| 232 | * @param int[]|string[] $ids |
| 233 | * @return \Illuminate\Database\Eloquent\Builder |
| 234 | */ |
| 235 | protected function newQueryForCollectionRestoration(array $ids) |
| 236 | { |
| 237 | $ids = \array_values($ids); |
| 238 | if (!\str_contains($ids[0], ':')) { |
| 239 | return parent::newQueryForRestoration($ids); |
| 240 | } |
| 241 | $query = $this->newQueryWithoutScopes(); |
| 242 | foreach ($ids as $id) { |
| 243 | $segments = \explode(':', $id); |
| 244 | $query->orWhere(function ($query) use($segments) { |
| 245 | return $query->where($segments[0], $segments[1])->where($segments[2], $segments[3]); |
| 246 | }); |
| 247 | } |
| 248 | return $query; |
| 249 | } |
| 250 | /** |
| 251 | * Unset all the loaded relations for the instance. |
| 252 | * |
| 253 | * @return $this |
| 254 | */ |
| 255 | public function unsetRelations() |
| 256 | { |
| 257 | $this->pivotParent = null; |
| 258 | $this->relations = []; |
| 259 | return $this; |
| 260 | } |
| 261 | } |
| 262 |