|
3 | 3 | namespace Tempest\Database\Builder\QueryBuilders; |
4 | 4 |
|
5 | 5 | use Tempest\Database\BelongsTo; |
| 6 | +use Tempest\Database\BelongsToMany; |
6 | 7 | use Tempest\Database\Builder\ModelInspector; |
7 | 8 | use Tempest\Database\Builder\WhereOperator; |
8 | 9 | use Tempest\Database\Database; |
|
12 | 13 | use Tempest\Database\Exceptions\HasOneRelationCouldNotBeUpdated; |
13 | 14 | use Tempest\Database\Exceptions\ModelDidNotHavePrimaryColumn; |
14 | 15 | use Tempest\Database\HasMany; |
| 16 | +use Tempest\Database\HasManyThrough; |
15 | 17 | use Tempest\Database\HasOne; |
| 18 | +use Tempest\Database\HasOneThrough; |
16 | 19 | use Tempest\Database\OnDatabase; |
17 | 20 | use Tempest\Database\PrimaryKey; |
18 | 21 | use Tempest\Database\Query; |
|
24 | 27 | use Tempest\Mapper\SerializerFactory; |
25 | 28 | use Tempest\Reflection\ClassReflector; |
26 | 29 | use Tempest\Reflection\PropertyReflector; |
| 30 | +use Tempest\Support\Arr; |
27 | 31 | use Tempest\Support\Arr\ImmutableArray; |
28 | 32 | use Tempest\Support\Conditions\HasConditions; |
29 | 33 | use Tempest\Support\Str\ImmutableString; |
@@ -225,7 +229,13 @@ private function resolveValuesToUpdate(): ImmutableArray |
225 | 229 |
|
226 | 230 | private function handleRelationUpdate(string $column, mixed $value): bool |
227 | 231 | { |
228 | | - return $this->handleHasManyRelation($column, $value) || $this->handleHasOneRelation($column, $value); |
| 232 | + return ( |
| 233 | + $this->handleHasManyRelation(key: $column, relations: $value) |
| 234 | + || $this->handleHasOneRelation(key: $column, relation: $value) |
| 235 | + || $this->handleBelongsToManyRelation(key: $column, relations: $value) |
| 236 | + || $this->handleHasManyThroughRelation(key: $column) |
| 237 | + || $this->handleHasOneThroughRelation(key: $column) |
| 238 | + ); |
229 | 239 | } |
230 | 240 |
|
231 | 241 | private function resolvePropertyValue(PropertyReflector $property, string $column, mixed $value): array |
@@ -323,6 +333,99 @@ private function handleHasOneRelation(string $key, mixed $relation): bool |
323 | 333 | return true; |
324 | 334 | } |
325 | 335 |
|
| 336 | + private function handleBelongsToManyRelation(string $key, mixed $relations): bool |
| 337 | + { |
| 338 | + $belongsToMany = $this->model->getBelongsToMany(name: $key); |
| 339 | + |
| 340 | + if (! $belongsToMany instanceof BelongsToMany) { |
| 341 | + return false; |
| 342 | + } |
| 343 | + |
| 344 | + if (is_iterable($relations)) { |
| 345 | + $this->addBelongsToManyRelationCallback(relationName: $key, relations: $relations); |
| 346 | + } |
| 347 | + |
| 348 | + return true; |
| 349 | + } |
| 350 | + |
| 351 | + private function handleHasManyThroughRelation(string $key): bool |
| 352 | + { |
| 353 | + return $this->model->getHasManyThrough(name: $key) instanceof HasManyThrough; |
| 354 | + } |
| 355 | + |
| 356 | + private function handleHasOneThroughRelation(string $key): bool |
| 357 | + { |
| 358 | + return $this->model->getHasOneThrough(name: $key) instanceof HasOneThrough; |
| 359 | + } |
| 360 | + |
| 361 | + private function addBelongsToManyRelationCallback(string $relationName, iterable $relations): void |
| 362 | + { |
| 363 | + $belongsToMany = $this->model->getBelongsToMany(name: $relationName); |
| 364 | + |
| 365 | + if (! $belongsToMany instanceof BelongsToMany) { |
| 366 | + return; |
| 367 | + } |
| 368 | + |
| 369 | + $this->ensureModelHasPrimaryKey(model: $this->model, relationType: 'BelongsToMany'); |
| 370 | + |
| 371 | + $this->after[] = function (PrimaryKey $parentId) use ($belongsToMany, $relations) { |
| 372 | + $ownerModel = inspect(model: $this->model->getName()); |
| 373 | + $targetModel = inspect(model: $belongsToMany->property->getIterableType()->asClass()); |
| 374 | + |
| 375 | + $pivotTable = $belongsToMany->pivot ?? implode(separator: '_', array: Arr\sort(array: [$ownerModel->getTableName(), $targetModel->getTableName()])); |
| 376 | + |
| 377 | + $ownerFk = $belongsToMany->ownerJoin |
| 378 | + ? $this->removeTablePrefix(column: $belongsToMany->ownerJoin) |
| 379 | + : Intl\singularize_last_word(value: $ownerModel->getTableName()) . '_' . $ownerModel->getPrimaryKey(); |
| 380 | + |
| 381 | + $targetPk = $targetModel->getPrimaryKey(); |
| 382 | + |
| 383 | + if (! $targetPk) { |
| 384 | + throw ModelDidNotHavePrimaryColumn::neededForRelation(model: $targetModel->getName(), relationType: 'BelongsToMany'); |
| 385 | + } |
| 386 | + |
| 387 | + $targetFk = $belongsToMany->relatedOwnerJoin |
| 388 | + ? $this->removeTablePrefix(column: $belongsToMany->relatedOwnerJoin) |
| 389 | + : Intl\singularize_last_word(value: $targetModel->getTableName()) . '_' . $targetPk; |
| 390 | + |
| 391 | + // Delete existing pivot rows |
| 392 | + new DeleteQueryBuilder(model: $pivotTable) |
| 393 | + ->whereField(field: $ownerFk, value: $parentId->value) |
| 394 | + ->build() |
| 395 | + ->onDatabase($this->onDatabase) |
| 396 | + ->execute(); |
| 397 | + |
| 398 | + $pivotRows = []; |
| 399 | + |
| 400 | + foreach ($relations as $related) { |
| 401 | + $relatedId = match (true) { |
| 402 | + is_object($related) && isset($related->{$targetPk}) => $related->{$targetPk}, |
| 403 | + is_array($related) && isset($related[$targetPk]) => $related[$targetPk], |
| 404 | + default => new InsertQueryBuilder( |
| 405 | + model: $targetModel->getName(), |
| 406 | + rows: [$related], |
| 407 | + serializerFactory: $this->serializerFactory, |
| 408 | + )->execute(), |
| 409 | + }; |
| 410 | + |
| 411 | + $pivotRows[] = [ |
| 412 | + $ownerFk => $parentId, |
| 413 | + $targetFk => $relatedId, |
| 414 | + ]; |
| 415 | + } |
| 416 | + |
| 417 | + if ($pivotRows === []) { |
| 418 | + return null; |
| 419 | + } |
| 420 | + |
| 421 | + return new InsertQueryBuilder( |
| 422 | + model: $pivotTable, |
| 423 | + rows: $pivotRows, |
| 424 | + serializerFactory: $this->serializerFactory, |
| 425 | + ); |
| 426 | + }; |
| 427 | + } |
| 428 | + |
326 | 429 | private function addHasManyRelationCallback(string $relationName, iterable $relations): void |
327 | 430 | { |
328 | 431 | $hasMany = $this->model->getHasMany($relationName); |
@@ -600,7 +703,13 @@ private function hasRelationUpdates(): bool |
600 | 703 |
|
601 | 704 | private function isRelationField(string $field): bool |
602 | 705 | { |
603 | | - return $this->model->getHasMany($field) || $this->model->getHasOne($field); |
| 706 | + return ( |
| 707 | + $this->model->getHasMany(name: $field) |
| 708 | + || $this->model->getHasOne(name: $field) |
| 709 | + || $this->model->getBelongsToMany(name: $field) instanceof BelongsToMany |
| 710 | + || $this->model->getHasManyThrough(name: $field) instanceof HasManyThrough |
| 711 | + || $this->model->getHasOneThrough(name: $field) instanceof HasOneThrough |
| 712 | + ); |
604 | 713 | } |
605 | 714 |
|
606 | 715 | private function validateRelationUpdateConstraints(): void |
|
0 commit comments