Skip to content

Commit 9dc8705

Browse files
committed
fix to pass original entity for table column render
1 parent 4a95e50 commit 9dc8705

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

demos/init-db.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,15 @@ protected function init(): void
270270
'type' => 'string',
271271
'ui' => [
272272
'form' => [Form\Control\Line::class],
273-
'table' => [Table\Column\CountryFlag::class],
273+
'table' => [Table\Column\CountryFlag::class, 'nameField' => $this->fieldName()->client_country],
274274
],
275275
])
276-
->addField($this->fieldName()->client_country, Country::hinting()->fieldName()->name);
276+
->addField($this->fieldName()->client_country, Country::hinting()->fieldName()->name, [
277+
// caption needs to be set explicitly because HasOneSql::addField() uses the value from referenced field
278+
// https://github.com/atk4/data/blob/4.0.0/src/Reference/HasOneSql.php#L87
279+
// https://github.com/atk4/data/pull/447/files#r1063408315
280+
'caption' => 'Client Country Name',
281+
]);
277282

278283
$this->addField($this->fieldName()->is_commercial, ['type' => 'boolean']);
279284
$this->addField($this->fieldName()->currency, ['values' => ['EUR' => 'Euro', 'USD' => 'US Dollar', 'GBP' => 'Pound Sterling']]);

src/Table.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public function setModel(Model $model, array $columns = null): void
392392

393393
public function setSource(array $data, $fields = null): Model
394394
{
395-
// mainly for CardTable to support different field type for different row 1/2
395+
// mainly for CardTable to support different field type for different row
396396
// related with https://github.com/atk4/data/blob/4.0.0/tests/Persistence/StaticTest.php#L142
397397
$dataWithObjects = [];
398398
if (is_array(reset($data))) {
@@ -455,26 +455,9 @@ protected function renderView(): void
455455
// the same in Lister class
456456
$modelBackup = $this->model;
457457
try {
458-
foreach ($this->model as $entityOrig) {
459-
// mainly for CardTable to support different field type for different row 2/2
460-
$entityCloned = (clone $entityOrig->getModel())->createEntity();
461-
$entityCloned->setId($entityOrig->getId());
462-
\Closure::bind(function () use ($entityOrig, $entityCloned) {
463-
foreach ($entityOrig->data as $k => $v) {
464-
$field = $entityCloned->getField($k);
465-
if ($field->type === 'atk4_local_object' && $v instanceof EntityFieldPair) {
466-
$field->type = $v->getField()->type;
467-
$field->enum = $v->getField()->enum;
468-
$field->values = $v->getField()->values;
469-
$field->ui = $v->getField()->ui;
470-
$v = $v->get();
471-
}
472-
$entityCloned->data[$k] = $v;
473-
}
474-
}, null, Model::class)();
475-
476-
$this->model = $entityCloned;
477-
$this->currentRow = $entityCloned; // TODO we should either drop currentRow property or never update model property
458+
foreach ($this->model as $entity) {
459+
$this->model = $entity;
460+
$this->currentRow = $entity; // TODO we should either drop currentRow property or never update model property
478461
if ($this->hook(self::HOOK_BEFORE_ROW) === false) {
479462
continue;
480463
}

src/Table/Column/Multiformat.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public function getDataCellHtml(Field $field = null, array $attr = []): string
3131

3232
public function getHtmlTags(Model $row, ?Field $field): array
3333
{
34+
// mainly for CardTable to support different field type for different row
35+
$fieldValue = $field->get($row);
36+
if ($fieldValue instanceof Model\EntityFieldPair) {
37+
$row = $fieldValue->getEntity();
38+
$field = $fieldValue->getField();
39+
}
40+
3441
$decorators = ($this->callback)($row, $field);
3542
// we need to smartly wrap things up
3643
$name = $field->shortName;

0 commit comments

Comments
 (0)