From d639bc958e5bf2d2f6827a1acbf7527101d565ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 12 Jan 2025 00:43:36 +0100 Subject: [PATCH] Improve HasOneSql::addField() implicit caption --- src/Reference/HasOneSql.php | 11 +++++++++-- tests/ReferenceSqlTest.php | 32 +++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Reference/HasOneSql.php b/src/Reference/HasOneSql.php index 0a3fed511..0f228480e 100644 --- a/src/Reference/HasOneSql.php +++ b/src/Reference/HasOneSql.php @@ -64,6 +64,13 @@ private function _addField(string $fieldName, bool $theirFieldIsTitle, ?string $ return $fieldExpression; } + private function getOurFieldCaptionWithoutReferenceSuffix(Model $theirModel): string + { + $theirField = $theirModel->getField($this->getTheirFieldName($theirModel)); + + return preg_replace('~ (' . preg_quote($theirField->getCaption(), '~') . '|ID)$~i', '', $this->getOurField()->getCaption()); + } + /** * Creates expression which sub-selects a field inside related model. * @@ -76,14 +83,14 @@ public function addField(string $fieldName, ?string $theirFieldName = null, arra } $ourModel = $this->getOurModel(); + $analysingTheirModel = $ourModel->getReference($this->link)->createAnalysingTheirModel(); // if caption/type is not defined in $defaults then infer it from their field - $analysingTheirModel = $ourModel->getReference($this->link)->createAnalysingTheirModel(); $analysingTheirField = $analysingTheirModel->getField($theirFieldName); $defaults['type'] ??= $analysingTheirField->type; $defaults['enum'] ??= $analysingTheirField->enum; $defaults['values'] ??= $analysingTheirField->values; - $defaults['caption'] ??= $analysingTheirField->caption; + $defaults['caption'] ??= $this->getOurFieldCaptionWithoutReferenceSuffix($analysingTheirModel) . ' ' . $analysingTheirField->getCaption(); $defaults['ui'] = array_merge($defaults['ui'] ?? $analysingTheirField->ui, ['editable' => false]); $fieldExpression = $this->_addField($fieldName, false, $theirFieldName, $defaults); diff --git a/tests/ReferenceSqlTest.php b/tests/ReferenceSqlTest.php index 0a03f589d..3f583f038 100644 --- a/tests/ReferenceSqlTest.php +++ b/tests/ReferenceSqlTest.php @@ -927,12 +927,38 @@ public function testHasOneReferenceCaption(): void $orderUserRef = $o->hasOne('my_user', ['model' => $u, 'ourField' => 'user_id']); $orderUserRef->addField('user_last_name', 'last_name'); - $referencedCaption = $o->getField('user_last_name')->getCaption(); - // $field->caption for the field 'last_name' is defined in referenced model (User) // When Order add field from Referenced model User // caption will be passed to Order field user_last_name - self::assertSame('Surname', $referencedCaption); + $referencedCaption = $o->getField('user_last_name')->getCaption(); + self::assertSame('User Surname', $referencedCaption); + } + + public function testHasOneReferenceCaptionNonIdField(): void + { + $this->setDb([ + 'user' => [ + 1 => ['id' => 1, 'name' => 'John', 'last_name' => 'Doe'], + ['id' => 2, 'name' => 'Peter', 'last_name' => 'Foo'], + ['id' => 3, 'name' => 'Goofy', 'last_name' => 'Goo'], + ], + 'order' => [ + 1 => ['id' => 1, 'user_name' => 'John'], + ['id' => 2, 'user_name' => 'Peter'], + ['id' => 3, 'user_name' => 'John'], + ], + ]); + + $u = new Model($this->db, ['table' => 'user']); + $u->addField('name'); + $u->addField('last_name', ['caption' => 'Surname']); + + $o = (new Model($this->db, ['table' => 'order'])); + $orderUserRef = $o->hasOne('my_user', ['model' => $u, 'ourField' => 'user_name', 'theirField' => 'name']); + $orderUserRef->addField('user_last_name', 'last_name'); + + $referencedCaption = $o->getField('user_last_name')->getCaption(); + self::assertSame('User Surname', $referencedCaption); } /**