Skip to content

Commit

Permalink
for title as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 12, 2025
1 parent 084276e commit 9cd1437
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ private function _addField(string $fieldName, bool $theirFieldIsTitle, ?string $
return $fieldExpression;
}

private function getLinkNameWithoutIdSuffix(): string
private function getLinkNameWithoutReferenceSuffix(Model $theirModel): string
{
$ourModel = $this->getOurModel();
$theirFieldName = $this->getTheirFieldName($theirModel);

return preg_replace('~_(' . preg_quote($ourModel->idField, '~') . '|id)$~', '', $this->link);
return preg_replace('~_(' . preg_quote($theirFieldName, '~') . '|' . preg_quote($ourModel->idField, '~') . '|id)$~', '', $this->link);
}

private function getOurFieldCaptionWithoutReferenceSuffix(Model $theirModel): string
Expand Down Expand Up @@ -182,7 +183,10 @@ public function refLink(array $defaults = []): Model
*/
public function addTitle(array $defaults = []): SqlExpressionField
{
$fieldName = $defaults['field'] ?? $this->getLinkNameWithoutIdSuffix();
$ourModel = $this->getOurModel();
$analysingTheirModel = $ourModel->getReference($this->link)->createAnalysingTheirModel();

$fieldName = $defaults['field'] ?? $this->getLinkNameWithoutReferenceSuffix($analysingTheirModel);

$defaults['ui'] = array_merge(['visible' => true], $defaults['ui'] ?? [], ['editable' => false]);

Expand Down
16 changes: 13 additions & 3 deletions tests/ReferenceSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,10 @@ public function testAddTitleWholeIdField(): void
$o->addField('amount');

$o->hasOne('user_id', ['model' => $u]);
$titleField = $o->getReference('user_id')->addTitle();
$referencedTitleField = $o->getReference('user_id')->addTitle();

self::assertSame('user', $titleField->shortName);
self::assertSame('User', $titleField->getCaption());
self::assertSame('user', $referencedTitleField->shortName);
self::assertSame('User', $referencedTitleField->getCaption());
}

/**
Expand Down Expand Up @@ -981,6 +981,16 @@ public function testHasOneReferenceCaptionNonIdField(): void

$referencedCaption = $o->getField('user_last_name')->getCaption();
self::assertSame('User Surname', $referencedCaption);

$referencedTitleField = $orderUserRef->addTitle();
self::assertSame('my_user', $referencedTitleField->shortName);
self::assertSame('My User', $referencedTitleField->getCaption());

$orderUserRef2 = $o->hasOne('my_user2_name', ['model' => $u, 'ourField' => 'user_name', 'theirField' => 'name']);
$referenced2TitleField = $orderUserRef2->addTitle();

self::assertSame('my_user2', $referenced2TitleField->shortName);
self::assertSame('My User 2', $referenced2TitleField->getCaption());
}

/**
Expand Down

0 comments on commit 9cd1437

Please sign in to comment.