Concerns
1 month ago
BelongsTo.php
2 years ago
BelongsToMany.php
1 month ago
HasMany.php
2 years ago
HasManyThrough.php
1 month ago
HasOne.php
1 month ago
HasOneOrMany.php
1 month ago
HasOneThrough.php
2 years ago
MorphMany.php
2 years ago
MorphOne.php
1 month ago
MorphOneOrMany.php
2 years ago
MorphPivot.php
1 month ago
MorphTo.php
6 months ago
MorphToMany.php
2 years ago
Pivot.php
2 years ago
Relation.php
1 month ago
HasOneOrMany.php
421 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent\Relations; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Eloquent\Builder; |
| 6 | use IAWPSCOPED\Illuminate\Database\Eloquent\Collection; |
| 7 | use IAWPSCOPED\Illuminate\Database\Eloquent\Model; |
| 8 | use IAWPSCOPED\Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; |
| 9 | /** @internal */ |
| 10 | abstract class HasOneOrMany extends Relation |
| 11 | { |
| 12 | use InteractsWithDictionary; |
| 13 | /** |
| 14 | * The foreign key of the parent model. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $foreignKey; |
| 19 | /** |
| 20 | * The local key of the parent model. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $localKey; |
| 25 | /** |
| 26 | * Create a new has one or many relationship instance. |
| 27 | * |
| 28 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 29 | * @param \Illuminate\Database\Eloquent\Model $parent |
| 30 | * @param string $foreignKey |
| 31 | * @param string $localKey |
| 32 | * @return void |
| 33 | */ |
| 34 | public function __construct(Builder $query, Model $parent, $foreignKey, $localKey) |
| 35 | { |
| 36 | $this->localKey = $localKey; |
| 37 | $this->foreignKey = $foreignKey; |
| 38 | parent::__construct($query, $parent); |
| 39 | } |
| 40 | /** |
| 41 | * Create and return an un-saved instance of the related model. |
| 42 | * |
| 43 | * @param array $attributes |
| 44 | * @return \Illuminate\Database\Eloquent\Model |
| 45 | */ |
| 46 | public function make(array $attributes = []) |
| 47 | { |
| 48 | return \IAWPSCOPED\tap($this->related->newInstance($attributes), function ($instance) { |
| 49 | $this->setForeignAttributesForCreate($instance); |
| 50 | }); |
| 51 | } |
| 52 | /** |
| 53 | * Create and return an un-saved instance of the related models. |
| 54 | * |
| 55 | * @param iterable $records |
| 56 | * @return \Illuminate\Database\Eloquent\Collection |
| 57 | */ |
| 58 | public function makeMany($records) |
| 59 | { |
| 60 | $instances = $this->related->newCollection(); |
| 61 | foreach ($records as $record) { |
| 62 | $instances->push($this->make($record)); |
| 63 | } |
| 64 | return $instances; |
| 65 | } |
| 66 | /** |
| 67 | * Set the base constraints on the relation query. |
| 68 | * |
| 69 | * @return void |
| 70 | */ |
| 71 | public function addConstraints() |
| 72 | { |
| 73 | if (static::$constraints) { |
| 74 | $query = $this->getRelationQuery(); |
| 75 | $query->where($this->foreignKey, '=', $this->getParentKey()); |
| 76 | $query->whereNotNull($this->foreignKey); |
| 77 | } |
| 78 | } |
| 79 | /** |
| 80 | * Set the constraints for an eager load of the relation. |
| 81 | * |
| 82 | * @param array $models |
| 83 | * @return void |
| 84 | */ |
| 85 | public function addEagerConstraints(array $models) |
| 86 | { |
| 87 | $whereIn = $this->whereInMethod($this->parent, $this->localKey); |
| 88 | $this->getRelationQuery()->{$whereIn}($this->foreignKey, $this->getKeys($models, $this->localKey)); |
| 89 | } |
| 90 | /** |
| 91 | * Match the eagerly loaded results to their single parents. |
| 92 | * |
| 93 | * @param array $models |
| 94 | * @param \Illuminate\Database\Eloquent\Collection $results |
| 95 | * @param string $relation |
| 96 | * @return array |
| 97 | */ |
| 98 | public function matchOne(array $models, Collection $results, $relation) |
| 99 | { |
| 100 | return $this->matchOneOrMany($models, $results, $relation, 'one'); |
| 101 | } |
| 102 | /** |
| 103 | * Match the eagerly loaded results to their many parents. |
| 104 | * |
| 105 | * @param array $models |
| 106 | * @param \Illuminate\Database\Eloquent\Collection $results |
| 107 | * @param string $relation |
| 108 | * @return array |
| 109 | */ |
| 110 | public function matchMany(array $models, Collection $results, $relation) |
| 111 | { |
| 112 | return $this->matchOneOrMany($models, $results, $relation, 'many'); |
| 113 | } |
| 114 | /** |
| 115 | * Match the eagerly loaded results to their many parents. |
| 116 | * |
| 117 | * @param array $models |
| 118 | * @param \Illuminate\Database\Eloquent\Collection $results |
| 119 | * @param string $relation |
| 120 | * @param string $type |
| 121 | * @return array |
| 122 | */ |
| 123 | protected function matchOneOrMany(array $models, Collection $results, $relation, $type) |
| 124 | { |
| 125 | $dictionary = $this->buildDictionary($results); |
| 126 | // Once we have the dictionary we can simply spin through the parent models to |
| 127 | // link them up with their children using the keyed dictionary to make the |
| 128 | // matching very convenient and easy work. Then we'll just return them. |
| 129 | foreach ($models as $model) { |
| 130 | if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) { |
| 131 | $model->setRelation($relation, $this->getRelationValue($dictionary, $key, $type)); |
| 132 | } |
| 133 | } |
| 134 | return $models; |
| 135 | } |
| 136 | /** |
| 137 | * Get the value of a relationship by one or many type. |
| 138 | * |
| 139 | * @param array $dictionary |
| 140 | * @param string $key |
| 141 | * @param string $type |
| 142 | * @return mixed |
| 143 | */ |
| 144 | protected function getRelationValue(array $dictionary, $key, $type) |
| 145 | { |
| 146 | $value = $dictionary[$key]; |
| 147 | return $type === 'one' ? \reset($value) : $this->related->newCollection($value); |
| 148 | } |
| 149 | /** |
| 150 | * Build model dictionary keyed by the relation's foreign key. |
| 151 | * |
| 152 | * @param \Illuminate\Database\Eloquent\Collection $results |
| 153 | * @return array |
| 154 | */ |
| 155 | protected function buildDictionary(Collection $results) |
| 156 | { |
| 157 | $foreign = $this->getForeignKeyName(); |
| 158 | return $results->mapToDictionary(function ($result) use($foreign) { |
| 159 | return [$this->getDictionaryKey($result->{$foreign}) => $result]; |
| 160 | })->all(); |
| 161 | } |
| 162 | /** |
| 163 | * Find a model by its primary key or return a new instance of the related model. |
| 164 | * |
| 165 | * @param mixed $id |
| 166 | * @param array $columns |
| 167 | * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model |
| 168 | */ |
| 169 | public function findOrNew($id, $columns = ['*']) |
| 170 | { |
| 171 | if (\is_null($instance = $this->find($id, $columns))) { |
| 172 | $instance = $this->related->newInstance(); |
| 173 | $this->setForeignAttributesForCreate($instance); |
| 174 | } |
| 175 | return $instance; |
| 176 | } |
| 177 | /** |
| 178 | * Get the first related model record matching the attributes or instantiate it. |
| 179 | * |
| 180 | * @param array $attributes |
| 181 | * @param array $values |
| 182 | * @return \Illuminate\Database\Eloquent\Model |
| 183 | */ |
| 184 | public function firstOrNew(array $attributes = [], array $values = []) |
| 185 | { |
| 186 | if (\is_null($instance = $this->where($attributes)->first())) { |
| 187 | $instance = $this->related->newInstance(\array_merge($attributes, $values)); |
| 188 | $this->setForeignAttributesForCreate($instance); |
| 189 | } |
| 190 | return $instance; |
| 191 | } |
| 192 | /** |
| 193 | * Get the first related record matching the attributes or create it. |
| 194 | * |
| 195 | * @param array $attributes |
| 196 | * @param array $values |
| 197 | * @return \Illuminate\Database\Eloquent\Model |
| 198 | */ |
| 199 | public function firstOrCreate(array $attributes = [], array $values = []) |
| 200 | { |
| 201 | if (\is_null($instance = $this->where($attributes)->first())) { |
| 202 | $instance = $this->create(\array_merge($attributes, $values)); |
| 203 | } |
| 204 | return $instance; |
| 205 | } |
| 206 | /** |
| 207 | * Create or update a related record matching the attributes, and fill it with values. |
| 208 | * |
| 209 | * @param array $attributes |
| 210 | * @param array $values |
| 211 | * @return \Illuminate\Database\Eloquent\Model |
| 212 | */ |
| 213 | public function updateOrCreate(array $attributes, array $values = []) |
| 214 | { |
| 215 | return \IAWPSCOPED\tap($this->firstOrNew($attributes), function ($instance) use($values) { |
| 216 | $instance->fill($values); |
| 217 | $instance->save(); |
| 218 | }); |
| 219 | } |
| 220 | /** |
| 221 | * Attach a model instance to the parent model. |
| 222 | * |
| 223 | * @param \Illuminate\Database\Eloquent\Model $model |
| 224 | * @return \Illuminate\Database\Eloquent\Model|false |
| 225 | */ |
| 226 | public function save(Model $model) |
| 227 | { |
| 228 | $this->setForeignAttributesForCreate($model); |
| 229 | return $model->save() ? $model : \false; |
| 230 | } |
| 231 | /** |
| 232 | * Attach a model instance without raising any events to the parent model. |
| 233 | * |
| 234 | * @param \Illuminate\Database\Eloquent\Model $model |
| 235 | * @return \Illuminate\Database\Eloquent\Model|false |
| 236 | */ |
| 237 | public function saveQuietly(Model $model) |
| 238 | { |
| 239 | return Model::withoutEvents(function () use($model) { |
| 240 | return $this->save($model); |
| 241 | }); |
| 242 | } |
| 243 | /** |
| 244 | * Attach a collection of models to the parent instance. |
| 245 | * |
| 246 | * @param iterable $models |
| 247 | * @return iterable |
| 248 | */ |
| 249 | public function saveMany($models) |
| 250 | { |
| 251 | foreach ($models as $model) { |
| 252 | $this->save($model); |
| 253 | } |
| 254 | return $models; |
| 255 | } |
| 256 | /** |
| 257 | * Attach a collection of models to the parent instance without raising any events to the parent model. |
| 258 | * |
| 259 | * @param iterable $models |
| 260 | * @return iterable |
| 261 | */ |
| 262 | public function saveManyQuietly($models) |
| 263 | { |
| 264 | return Model::withoutEvents(function () use($models) { |
| 265 | return $this->saveMany($models); |
| 266 | }); |
| 267 | } |
| 268 | /** |
| 269 | * Create a new instance of the related model. |
| 270 | * |
| 271 | * @param array $attributes |
| 272 | * @return \Illuminate\Database\Eloquent\Model |
| 273 | */ |
| 274 | public function create(array $attributes = []) |
| 275 | { |
| 276 | return \IAWPSCOPED\tap($this->related->newInstance($attributes), function ($instance) { |
| 277 | $this->setForeignAttributesForCreate($instance); |
| 278 | $instance->save(); |
| 279 | }); |
| 280 | } |
| 281 | /** |
| 282 | * Create a new instance of the related model without raising any events to the parent model. |
| 283 | * |
| 284 | * @param array $attributes |
| 285 | * @return \Illuminate\Database\Eloquent\Model |
| 286 | */ |
| 287 | public function createQuietly(array $attributes = []) |
| 288 | { |
| 289 | return Model::withoutEvents(fn() => $this->create($attributes)); |
| 290 | } |
| 291 | /** |
| 292 | * Create a new instance of the related model. Allow mass-assignment. |
| 293 | * |
| 294 | * @param array $attributes |
| 295 | * @return \Illuminate\Database\Eloquent\Model |
| 296 | */ |
| 297 | public function forceCreate(array $attributes = []) |
| 298 | { |
| 299 | $attributes[$this->getForeignKeyName()] = $this->getParentKey(); |
| 300 | return $this->related->forceCreate($attributes); |
| 301 | } |
| 302 | /** |
| 303 | * Create a Collection of new instances of the related model. |
| 304 | * |
| 305 | * @param iterable $records |
| 306 | * @return \Illuminate\Database\Eloquent\Collection |
| 307 | */ |
| 308 | public function createMany(iterable $records) |
| 309 | { |
| 310 | $instances = $this->related->newCollection(); |
| 311 | foreach ($records as $record) { |
| 312 | $instances->push($this->create($record)); |
| 313 | } |
| 314 | return $instances; |
| 315 | } |
| 316 | /** |
| 317 | * Create a Collection of new instances of the related model without raising any events to the parent model. |
| 318 | * |
| 319 | * @param iterable $records |
| 320 | * @return \Illuminate\Database\Eloquent\Collection |
| 321 | */ |
| 322 | public function createManyQuietly(iterable $records) |
| 323 | { |
| 324 | return Model::withoutEvents(fn() => $this->createMany($records)); |
| 325 | } |
| 326 | /** |
| 327 | * Set the foreign ID for creating a related model. |
| 328 | * |
| 329 | * @param \Illuminate\Database\Eloquent\Model $model |
| 330 | * @return void |
| 331 | */ |
| 332 | protected function setForeignAttributesForCreate(Model $model) |
| 333 | { |
| 334 | $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); |
| 335 | } |
| 336 | /** |
| 337 | * Add the constraints for a relationship query. |
| 338 | * |
| 339 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 340 | * @param \Illuminate\Database\Eloquent\Builder $parentQuery |
| 341 | * @param array|mixed $columns |
| 342 | * @return \Illuminate\Database\Eloquent\Builder |
| 343 | */ |
| 344 | public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
| 345 | { |
| 346 | if ($query->getQuery()->from == $parentQuery->getQuery()->from) { |
| 347 | return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); |
| 348 | } |
| 349 | return parent::getRelationExistenceQuery($query, $parentQuery, $columns); |
| 350 | } |
| 351 | /** |
| 352 | * Add the constraints for a relationship query on the same table. |
| 353 | * |
| 354 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 355 | * @param \Illuminate\Database\Eloquent\Builder $parentQuery |
| 356 | * @param array|mixed $columns |
| 357 | * @return \Illuminate\Database\Eloquent\Builder |
| 358 | */ |
| 359 | public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
| 360 | { |
| 361 | $query->from($query->getModel()->getTable() . ' as ' . ($hash = $this->getRelationCountHash())); |
| 362 | $query->getModel()->setTable($hash); |
| 363 | return $query->select($columns)->whereColumn($this->getQualifiedParentKeyName(), '=', $hash . '.' . $this->getForeignKeyName()); |
| 364 | } |
| 365 | /** |
| 366 | * Get the key for comparing against the parent key in "has" query. |
| 367 | * |
| 368 | * @return string |
| 369 | */ |
| 370 | public function getExistenceCompareKey() |
| 371 | { |
| 372 | return $this->getQualifiedForeignKeyName(); |
| 373 | } |
| 374 | /** |
| 375 | * Get the key value of the parent's local key. |
| 376 | * |
| 377 | * @return mixed |
| 378 | */ |
| 379 | public function getParentKey() |
| 380 | { |
| 381 | return $this->parent->getAttribute($this->localKey); |
| 382 | } |
| 383 | /** |
| 384 | * Get the fully qualified parent key name. |
| 385 | * |
| 386 | * @return string |
| 387 | */ |
| 388 | public function getQualifiedParentKeyName() |
| 389 | { |
| 390 | return $this->parent->qualifyColumn($this->localKey); |
| 391 | } |
| 392 | /** |
| 393 | * Get the plain foreign key. |
| 394 | * |
| 395 | * @return string |
| 396 | */ |
| 397 | public function getForeignKeyName() |
| 398 | { |
| 399 | $segments = \explode('.', $this->getQualifiedForeignKeyName()); |
| 400 | return \end($segments); |
| 401 | } |
| 402 | /** |
| 403 | * Get the foreign key for the relationship. |
| 404 | * |
| 405 | * @return string |
| 406 | */ |
| 407 | public function getQualifiedForeignKeyName() |
| 408 | { |
| 409 | return $this->foreignKey; |
| 410 | } |
| 411 | /** |
| 412 | * Get the local key for the relationship. |
| 413 | * |
| 414 | * @return string |
| 415 | */ |
| 416 | public function getLocalKeyName() |
| 417 | { |
| 418 | return $this->localKey; |
| 419 | } |
| 420 | } |
| 421 |