diff --git a/src/Model/ReferencesTrait.php b/src/Model/ReferencesTrait.php index 6ba7cd4b93..9ab8bd2b6a 100644 --- a/src/Model/ReferencesTrait.php +++ b/src/Model/ReferencesTrait.php @@ -155,7 +155,7 @@ public function ref(string $link, array $defaults = []): Model } /** - * Traverse reference and create their model but keep condition not materialized (for subquery actions). + * Traverse reference and create their model but keep reference condition not materialized (for subquery actions). * * @param array $defaults */ @@ -163,6 +163,6 @@ public function refLink(string $link, array $defaults = []): Model { $reference = $this->getModel(true)->getReference($link); - return $reference->refLink($this, $defaults); + return $reference->refLink($defaults); } } diff --git a/src/Model/Scope/Condition.php b/src/Model/Scope/Condition.php index 2816500ce9..6da32fcf43 100644 --- a/src/Model/Scope/Condition.php +++ b/src/Model/Scope/Condition.php @@ -287,12 +287,14 @@ public function negate(): self #[\Override] public function toWords(Model $model = null): string { - if ($model === null) { + if ($model !== null) { + $model->assertIsModel(); + } else { $model = $this->getModel(); - } - if ($model === null) { - throw new Exception('Condition must be associated with Model to convert to words'); + if ($model === null) { + throw new Exception('Condition must be associated with model to convert to words'); + } } $field = $this->fieldToWords($model); @@ -405,9 +407,7 @@ protected function valueToWords(Model $model, $value): string $title = null; if ($field instanceof Field && $field->hasReference()) { // make sure we set the value in the Model - $entity = $model->isEntity() - ? clone $model - : $model->createEntity(); + $entity = $model->createEntity(); $entity->set($field->shortName, $value); // then take the title diff --git a/src/Reference/HasMany.php b/src/Reference/HasMany.php index 253d41fe5d..f4c590f210 100644 --- a/src/Reference/HasMany.php +++ b/src/Reference/HasMany.php @@ -96,12 +96,8 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model * * @param array $defaults */ - public function refLink(?Model $ourModelOrEntity, array $defaults = []): Model + public function refLink(array $defaults = []): Model { - if ($ourModelOrEntity !== null) { // TODO drop this parameter, refLink for entity is useless - $this->assertOurModelOrEntity($ourModelOrEntity); - } - $theirModelLinked = $this->createTheirModel($defaults)->addCondition( $this->getTheirFieldName(), $this->referenceOurValue() @@ -136,7 +132,7 @@ public function addField(string $fieldName, array $defaults = []): Field if (isset($defaults['expr'])) { $fx = function () use ($defaults, $alias) { - $theirModelLinked = $this->refLink(null); + $theirModelLinked = $this->refLink(); return $theirModelLinked->action('field', [$theirModelLinked->expr( $defaults['expr'], @@ -146,15 +142,15 @@ public function addField(string $fieldName, array $defaults = []): Field unset($defaults['args']); } elseif (is_object($defaults['aggregate'])) { $fx = function () use ($defaults, $alias) { - return $this->refLink(null)->action('field', [$defaults['aggregate'], 'alias' => $alias]); + return $this->refLink()->action('field', [$defaults['aggregate'], 'alias' => $alias]); }; } elseif ($defaults['aggregate'] === 'count' && !isset($defaults['field'])) { $fx = function () use ($alias) { - return $this->refLink(null)->action('count', ['alias' => $alias]); + return $this->refLink()->action('count', ['alias' => $alias]); }; } elseif (in_array($defaults['aggregate'], ['sum', 'avg', 'min', 'max', 'count'], true)) { $fx = function () use ($defaults, $field) { - return $this->refLink(null)->action('fx0', [$defaults['aggregate'], $field]); + return $this->refLink()->action('fx0', [$defaults['aggregate'], $field]); }; } else { $fx = function () use ($defaults, $field) { @@ -163,7 +159,7 @@ public function addField(string $fieldName, array $defaults = []): Field $args['concatSeparator'] = $defaults['concatSeparator']; } - return $this->refLink(null)->action('fx', $args); + return $this->refLink()->action('fx', $args); }; } diff --git a/src/Reference/HasOneSql.php b/src/Reference/HasOneSql.php index b68ce4d07c..6af93666f1 100644 --- a/src/Reference/HasOneSql.php +++ b/src/Reference/HasOneSql.php @@ -147,7 +147,7 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model * * @param array $defaults */ - public function refLink(Model $ourModelOrEntity, array $defaults = []): Model + public function refLink(array $defaults = []): Model { $theirModel = $this->createTheirModel($defaults); diff --git a/tests/ScopeTest.php b/tests/ScopeTest.php index 5dfcd3812e..974dc118ec 100644 --- a/tests/ScopeTest.php +++ b/tests/ScopeTest.php @@ -216,7 +216,7 @@ public function testConditionUnsupportedToWords(): void $condition = new Condition('name', 'abc'); $this->expectException(Exception::class); - $this->expectExceptionMessage('Condition must be associated with Model to convert to words'); + $this->expectExceptionMessage('Condition must be associated with model to convert to words'); $condition->toWords(); }