|
7 | 7 | * @author Muhammet ŞAFAK <info@muhammetsafak.com.tr> |
8 | 8 | * @copyright Copyright © 2022 Muhammet ŞAFAK |
9 | 9 | * @license ./LICENSE MIT |
10 | | - * @version 2.0.7 |
| 10 | + * @version 2.0.8 |
11 | 11 | * @link https://www.muhammetsafak.com.tr |
12 | 12 | */ |
13 | 13 |
|
|
20 | 20 | ReadableException, |
21 | 21 | UpdatableException, |
22 | 22 | WritableException}; |
| 23 | +use InitPHP\Database\Helpers\Helper; |
23 | 24 | use InitPHP\Database\Helpers\Parameters; |
24 | 25 |
|
25 | 26 | abstract class Model extends Database |
@@ -457,58 +458,41 @@ final public function purgeDeleted(): bool |
457 | 458 | } |
458 | 459 |
|
459 | 460 | /** |
| 461 | + * İki model arasında bir JOIN ilişkisi kurar. |
| 462 | + * |
460 | 463 | * @param string|Model $model |
461 | | - * @param string|null $fromColumn |
462 | | - * @param string|null $targetColumn |
| 464 | + * @param string|null $foreignKey |
| 465 | + * @param string|null $localKey |
463 | 466 | * @param string $joinType |
464 | 467 | * @return $this |
465 | | - * @throws \ReflectionException |
466 | 468 | */ |
467 | | - final public function relation($model, ?string $fromColumn = null, ?string $targetColumn = null, string $joinType = 'INNER'): self |
| 469 | + final public function relation($model, ?string $foreignKey = null, ?string $localKey = null, string $joinType = 'INNER'): self |
468 | 470 | { |
469 | 471 | $from = [ |
470 | 472 | 'tableSchema' => $this->getSchema(), |
471 | 473 | 'tableSchemaID' => $this->getSchemaID(), |
472 | 474 | ]; |
473 | | - $ref = new \ReflectionClass($model); |
474 | | - if($ref->isSubclassOf(Model::class) === FALSE){ |
475 | | - throw new ModelRelationsException('The target class must be a subclass of \\InitPHP\\Database\\Model.'); |
476 | | - } |
477 | | - if(\defined('PHP_VERSION_ID') && \PHP_VERSION_ID >= 80000){ |
478 | | - $target = $ref->getProperty('table'); |
479 | | - $targetPrimaryKey = $ref->getProperty('primaryKey'); |
480 | | - if(($targetSchema = $target->getDefaultValue()) === null){ |
481 | | - $targetSchema = \strtolower($ref->getShortName()); |
482 | | - } |
483 | | - $targetSchemaID = $targetPrimaryKey->getDefaultValue(); |
484 | | - $target = [ |
485 | | - 'tableSchema' => $targetSchema, |
486 | | - 'tableSchemaID' => $targetSchemaID, |
487 | | - ]; |
488 | | - }else{ |
489 | | - if(!\is_object($model)){ |
490 | | - $model = $ref->newInstance(); |
491 | | - } |
492 | | - $target = [ |
493 | | - 'tableSchema' => $model->getSchema(), |
494 | | - 'tableSchemaID' => $model->getSchemaID(), |
495 | | - ]; |
496 | | - } |
497 | | - if($fromColumn === null || $fromColumn === '{primaryKey}'){ |
| 475 | + $target = $this->getModelTableNameAndPrimaryKeyColumn($model); |
| 476 | + |
| 477 | + if($localKey === null || $localKey === '{primaryKey}'){ |
498 | 478 | if(empty($from['tableSchemaID'])){ |
499 | 479 | throw new ModelRelationsException('To use relationships, the model must have a primary key column.'); |
500 | 480 | } |
501 | 481 | }else{ |
502 | | - $from['tableSchemaID'] = $fromColumn; |
| 482 | + $from['tableSchemaID'] = $localKey; |
503 | 483 | } |
504 | | - if($targetColumn === null || $targetColumn === '{primaryKey}'){ |
| 484 | + if($foreignKey === null) { |
| 485 | + $target['tableSchemaID'] = (Helper::str_ends_with($from['tableSchema'], 's') ? \substr($from['tableSchema'], -1) : $from['tableSchema']) |
| 486 | + . '_' . $from['tableSchemaID']; |
| 487 | + } elseif ($foreignKey === '{primaryKey}') { |
505 | 488 | if(empty($target['tableSchemaID'])){ |
506 | 489 | throw new ModelRelationsException('To use relationships, the model must have a primary key column.'); |
507 | 490 | } |
508 | 491 | }else{ |
509 | | - $target['tableSchemaID'] = $targetColumn; |
| 492 | + $target['tableSchemaID'] = $foreignKey; |
510 | 493 | } |
511 | 494 | $this->join($target['tableSchema'], ($from['tableSchema'] . '.' . $from['tableSchemaID'] . ' = ' . $target['tableSchema'] . '.' . $target['tableSchemaID']), $joinType); |
| 495 | + |
512 | 496 | return $this; |
513 | 497 | } |
514 | 498 |
|
@@ -603,4 +587,35 @@ private function callbacksFunctionHandler(array $data, string $method) |
603 | 587 | return $data; |
604 | 588 | } |
605 | 589 |
|
| 590 | + private function getModelTableNameAndPrimaryKeyColumn($model): array |
| 591 | + { |
| 592 | + $reflection = new \ReflectionClass($model); |
| 593 | + if ($reflection->isSubclassOf(Model::class) === FALSE) { |
| 594 | + throw new ModelRelationsException('The target class must be a subclass of \\InitPHP\\Database\\Model.'); |
| 595 | + } |
| 596 | + if (\defined('PHP_VERSION_ID') && \PHP_VERSION_ID >= 80000) { |
| 597 | + $tableReflection = $reflection->getProperty('table'); |
| 598 | + $primaryKeyReflection = $reflection->getProperty('primaryKey'); |
| 599 | + if (null === $tableName = $tableReflection->getDefaultValue()) { |
| 600 | + $tableName = \strtolower($reflection->getShortName()); |
| 601 | + } |
| 602 | + $primaryKey = $primaryKeyReflection->getDefaultValue(); |
| 603 | + |
| 604 | + return [ |
| 605 | + 'tableSchema' => $tableName, |
| 606 | + 'tableSchemaID' => $primaryKey, |
| 607 | + ]; |
| 608 | + } |
| 609 | + |
| 610 | + /** @var $model Model */ |
| 611 | + if (!\is_object($model)) { |
| 612 | + $model = $reflection->newInstance(); |
| 613 | + } |
| 614 | + |
| 615 | + return [ |
| 616 | + 'tableSchema' => $model->getSchema(), |
| 617 | + 'tableSchemaID' => $model->getSchemaID(), |
| 618 | + ]; |
| 619 | + } |
| 620 | + |
606 | 621 | } |
0 commit comments