From d1ec0c64dfdd44b204babebe94cd9aa3be3c70cd Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Sat, 1 Feb 2025 16:52:01 -0300 Subject: [PATCH 1/4] wip --- .../Eloquent/Relations/MorphOneOrMany.php | 45 ++++++++++++------- src/Illuminate/Database/Schema/Builder.php | 2 +- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index f964ff27c615..8f7a3272b13e 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -31,6 +31,14 @@ abstract class MorphOneOrMany extends HasOneOrMany */ protected $morphClass; + protected $morphKeyTypeString; + + public function morphKeyTypeString($morphKeyTypeString = true) + { + $this->morphKeyTypeString = $morphKeyTypeString; + return $this; + } + /** * Create a new morph one or many relationship instance. * @@ -58,22 +66,25 @@ public function __construct(Builder $query, Model $parent, $type, $id, $localKey #[\Override] public function addConstraints() { - if (static::$constraints) { - $this->getRelationQuery()->where($this->morphType, $this->morphClass); - - if (is_null(SchemaBuilder::$defaultMorphKeyType)) { - parent::addConstraints(); - } else { - $query = $this->getRelationQuery(); + if (! static::$constraints) { + return; + } - $query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) { - 'uuid', 'ulid', 'string' => (string) $key, - default => $key, - })); + $this->getRelationQuery()->where($this->morphType, $this->morphClass); - $query->whereNotNull($this->foreignKey); - } + if (SchemaBuilder::$defaultMorphKeyType === 'int') { + parent::addConstraints(); + return; } + + $query = $this->getRelationQuery(); + + $query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) { + 'uuid', 'ulid', 'string' => (string) $key, + default => $key, + })); + + $query->whereNotNull($this->foreignKey); } /** @inheritDoc */ @@ -198,7 +209,11 @@ protected function getPossibleInverseRelations(): array #[\Override] protected function getKeys(array $models, $key = null) { - $castKeyToString = in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']); + if (isset($this->morphKeyTypeString) && $this->morphKeyTypeString === true) { + $castKeyToString = true; + } else { + $castKeyToString = in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']); + } return (new Collection(parent::getKeys($models, $key))) ->transform(function ($key) use ($castKeyToString) { @@ -210,7 +225,7 @@ protected function getKeys(array $models, $key = null) #[\Override] protected function whereInMethod(Model $model, $key) { - if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string'])) { + if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']) && (!isset($this->morphKeyTypeString) || $this->morphKeyTypeString === false)) { return parent::whereInMethod($model, $key); } diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index bd1f2dcaab74..5643ec0171c5 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -46,7 +46,7 @@ class Builder * * @var string|null */ - public static $defaultMorphKeyType = null; + public static $defaultMorphKeyType = 'int'; /** * Create a new database Schema manager. From 39dcd5bb4f7672ceeafee94c09f5fab1dfa19ec2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 20 Feb 2025 13:46:36 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index 8f7a3272b13e..ccc7fb7b359c 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -36,6 +36,7 @@ abstract class MorphOneOrMany extends HasOneOrMany public function morphKeyTypeString($morphKeyTypeString = true) { $this->morphKeyTypeString = $morphKeyTypeString; + return $this; } @@ -74,6 +75,7 @@ public function addConstraints() if (SchemaBuilder::$defaultMorphKeyType === 'int') { parent::addConstraints(); + return; } @@ -225,7 +227,7 @@ protected function getKeys(array $models, $key = null) #[\Override] protected function whereInMethod(Model $model, $key) { - if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']) && (!isset($this->morphKeyTypeString) || $this->morphKeyTypeString === false)) { + if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']) && (! isset($this->morphKeyTypeString) || $this->morphKeyTypeString === false)) { return parent::whereInMethod($model, $key); } From 135b7420bb490bbc0ecb5329e23aac77db794dc2 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Mon, 24 Feb 2025 14:09:46 +0800 Subject: [PATCH 3/4] wip --- .../Eloquent/Relations/MorphOneOrMany.php | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index ccc7fb7b359c..b470fd57c99d 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -67,26 +67,24 @@ public function __construct(Builder $query, Model $parent, $type, $id, $localKey #[\Override] public function addConstraints() { - if (! static::$constraints) { - return; - } - - $this->getRelationQuery()->where($this->morphType, $this->morphClass); + if (static::$constraints) { + $this->getRelationQuery()->where($this->morphType, $this->morphClass); - if (SchemaBuilder::$defaultMorphKeyType === 'int') { - parent::addConstraints(); + if (SchemaBuilder::$defaultMorphKeyType === 'int') { + parent::addConstraints(); - return; - } + return; + } - $query = $this->getRelationQuery(); + $query = $this->getRelationQuery(); - $query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) { - 'uuid', 'ulid', 'string' => (string) $key, - default => $key, - })); + $query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) { + 'uuid', 'ulid', 'string' => (string) $key, + default => $key, + })); - $query->whereNotNull($this->foreignKey); + $query->whereNotNull($this->foreignKey); + } } /** @inheritDoc */ From 6d2cd10f41cf60af0816aeca565ed2ef19fbc094 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Mon, 24 Feb 2025 14:12:12 +0800 Subject: [PATCH 4/4] wip --- src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php | 2 +- src/Illuminate/Database/Schema/Builder.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index b470fd57c99d..2471a3f04c84 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -70,7 +70,7 @@ public function addConstraints() if (static::$constraints) { $this->getRelationQuery()->where($this->morphType, $this->morphClass); - if (SchemaBuilder::$defaultMorphKeyType === 'int') { + if (is_null(SchemaBuilder::$defaultMorphKeyType)) { parent::addConstraints(); return; diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 5643ec0171c5..bd1f2dcaab74 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -46,7 +46,7 @@ class Builder * * @var string|null */ - public static $defaultMorphKeyType = 'int'; + public static $defaultMorphKeyType = null; /** * Create a new database Schema manager.