Skip to content

Commit

Permalink
Improve HasOneSql::addField() implicit caption
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 11, 2025
1 parent 87a10f3 commit d639bc9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
32 changes: 29 additions & 3 deletions tests/ReferenceSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit d639bc9

Please sign in to comment.