Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HasOneSql::addField() implicit caption #1239

Merged
merged 8 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Persistence/Static_.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
class Static_ extends Array_
{
/** @var string This will be the title field for the model. */
public $titleFieldForModel;
public ?string $titleFieldForModel = null;

/** @var array<string, array<mixed>> Populate the following fields for the model. */
public $fieldsForModel = [];
public array $fieldsForModel;

/**
* @param array<int|string, mixed> $data
Expand Down Expand Up @@ -88,7 +88,7 @@ public function __construct(array $data = [])
}

// if title is not set, use first key
if (!$this->titleFieldForModel) {
if ($this->titleFieldForModel === null) {
if (is_int($k)) {
$keyOverride[$k] = 'name';
$this->titleFieldForModel = 'name';
Expand Down Expand Up @@ -162,7 +162,7 @@ public function add(Model $model, array $defaults = []): void
*/
protected function addMissingFieldsToModel(Model $model): void
{
if ($this->titleFieldForModel) {
if ($this->titleFieldForModel !== null) {
$model->titleField = $this->titleFieldForModel;
}

Expand Down
22 changes: 17 additions & 5 deletions src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ private function _addField(string $fieldName, bool $theirFieldIsTitle, ?string $
return $fieldExpression;
}

private function getLinkNameWithoutReferenceSuffix(): string
{
$ourModel = $this->getOurModel();

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

@mvorisek mvorisek Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced in e49806f, improved in 10b7ab1

I belive we should strip "our field id" as "their reference name" might strip the whole title/name.

}

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 +90,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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

$fieldExpression = $this->_addField($fieldName, false, $theirFieldName, $defaults);
Expand Down Expand Up @@ -167,9 +181,7 @@ public function refLink(array $defaults = []): Model
*/
public function addTitle(array $defaults = []): SqlExpressionField
{
$ourModel = $this->getOurModel();

$fieldName = $defaults['field'] ?? preg_replace('~_(' . preg_quote($ourModel->idField, '~') . '|id)$~', '', $this->link);
$fieldName = $defaults['field'] ?? $this->getLinkNameWithoutReferenceSuffix();

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

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
Loading