Skip to content

Commit

Permalink
Simplify Model::getIdField() method uses (#2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Feb 19, 2024
1 parent 21ac4a3 commit 6c609cf
Show file tree
Hide file tree
Showing 29 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion demos/basic/breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$model = new Country($app->db);
$model->setLimit(15);

$id = $app->uiPersistence->typecastAttributeLoadField($model->getField($model->idField), $crumb->stickyGet('country_id'));
$id = $app->uiPersistence->typecastAttributeLoadField($model->getIdField(), $crumb->stickyGet('country_id'));
if ($id !== null) {
// perhaps we edit individual country?
$model = $model->load($id);
Expand Down
4 changes: 2 additions & 2 deletions demos/collection/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@
$grid->addSelection();

$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $j, array $ids) use ($grid) {
return new JsToast('Selected: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getField($grid->model->idField), $id), $ids)) . '#');
return new JsToast('Selected: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getIdField(), $id), $ids)) . '#');
});

// executing a modal on a bulk selection
$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], '', static function (View $modal, array $ids) use ($grid) {
Message::addTo($modal, [
'The selected records will be permanently deleted: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getField($grid->model->idField), $id), $ids)) . '#',
'The selected records will be permanently deleted: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getIdField(), $id), $ids)) . '#',
'type' => 'warning',
'icon' => 'warning',
]);
Expand Down
6 changes: 3 additions & 3 deletions demos/collection/multitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private function explodeSelectionValue(string $value): array
{
$res = [];
foreach ($value === '' ? [] : explode(',', $value) as $v) {
$res[] = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->model->getField($this->model->idField), $v);
$res[] = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->model->getIdField(), $v);
}

return $res;
Expand Down Expand Up @@ -61,9 +61,9 @@ public function setModel(Model $model, array $route = []): void
$table->on('click', 'tr', $jsReload);

foreach ($selectionIds as $id) {
$table->js(true)->find('tr[data-id=' . $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $id) . ']')->addClass('active');
$table->js(true)->find('tr[data-id=' . $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $id) . ']')->addClass('active');

$path[] = $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $id);
$path[] = $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $id);
$pushModel = new $model($model->getPersistence());
$pushModel = $pushModel->load($id);

Expand Down
2 changes: 1 addition & 1 deletion demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected function init(): void

parent::init();

$this->getField($this->idField)->type = WrappedIdType::NAME;
$this->getIdField()->type = WrappedIdType::NAME;

$this->initPreventModification();

Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/jssortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@

$dragHandler = $grid->addDragHandler();
$dragHandler->onReorder(static function (array $orderedIds) use ($grid) {
return new JsToast('New order: ' . implode(' - ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getField($grid->model->idField), $id), $orderedIds)));
return new JsToast('New order: ' . implode(' - ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getIdField(), $id), $orderedIds)));
});
2 changes: 1 addition & 1 deletion demos/interactive/loader2.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@

$countryLoader->set(static function (Loader $p) {
$country = new Country($p->getApp()->db);
$id = $p->getApp()->uiPersistence->typecastAttributeLoadField($country->getField($country->idField), $p->getApp()->getRequestQueryParam('id'));
$id = $p->getApp()->uiPersistence->typecastAttributeLoadField($country->getIdField(), $p->getApp()->getRequestQueryParam('id'));
Form::addTo($p)->setModel($country->load($id));
});
2 changes: 1 addition & 1 deletion demos/layout/layout-panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
Header::addTo($app, ['UserAction Friendly', 'size' => 4, 'subHeader' => 'Panel can run model action.']);

$panel3 = Panel\Right::addTo($app);
$countryId = $app->uiPersistence->typecastAttributeLoadField($country->getField($country->idField), $panel3->stickyGet('id'));
$countryId = $app->uiPersistence->typecastAttributeLoadField($country->getIdField(), $panel3->stickyGet('id'));
$msg = Message::addTo($panel3, ['Run Country model action below.']);

$deck = View::addTo($app, ['ui' => 'cards']);
Expand Down
4 changes: 3 additions & 1 deletion src/Behat/RwDemosContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ protected function restoreDatabaseBackup(): void
}

foreach ([...$changes->updatedIds, ...$changes->deletedIds] as $id) {
$entity = in_array($id, $changes->updatedIds, true) ? $model->load($id) : $model->createEntity();
$entity = in_array($id, $changes->updatedIds, true)
? $model->load($id)
: $model->createEntity();
$entity->setMulti($data[$id]);
$entity->save();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function setModel(Model $entity, array $fields = null): void
$fields = array_keys($this->model->getFields(['editable', 'visible']));
}

$this->template->trySet('dataId', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $this->model->getId()));
$this->template->trySet('dataId', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $this->model->getId()));

View::addTo($this->getSection(), [$entity->getTitle(), 'class.header' => true]);
$this->getSection()->addFields($entity, $fields, $this->useLabel, $this->useTable);
Expand Down
2 changes: 1 addition & 1 deletion src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function getJsGridAction(Model\UserAction $action): ?JsExpressionable
case Model\UserAction::MODIFIER_DELETE:
// use deleted record ID to remove row, fallback to closest tr if ID is not available
$js = $this->deletedId
? $this->js(false, null, 'tr[data-id="' . $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $this->deletedId) . '"]')
? $this->js(false, null, 'tr[data-id="' . $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $this->deletedId) . '"]')
: (new Jquery())->closest('tr');
$js = $js->transition('fade left', new JsFunction([], [new JsExpression('this.remove()')]));

Expand Down
9 changes: 5 additions & 4 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Core\Factory;
use Atk4\Core\HookTrait;
use Atk4\Data\Exception as DataException;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Data\Model\EntityFieldPair;
Expand Down Expand Up @@ -444,15 +445,15 @@ protected function loadPost(): void
throw $e;
}

if ($e->getPrevious() !== null && $e instanceof DataException && $e->getMessage() === 'Typecast parse error') {
$e = $e->getPrevious();
}

$messages = [];
do {
$messages[] = $e->getMessage();
} while (($e = $e->getPrevious()) !== null);

if (count($messages) >= 2 && $messages[0] === 'Typecast parse error') {
array_shift($messages);
}

$errors[$k] = implode(': ', $messages);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function addControl(string $name, $control = [], array $fieldSeed = []):
} else {
$dropdownModel = $control instanceof Control ? $control->model : ($control['model'] ?? null);
if ($dropdownModel !== null) {
$fieldSeed['type'] = $dropdownModel->getField($dropdownModel->idField)->type;
$fieldSeed['type'] = $dropdownModel->getIdField()->type;
}
}
} elseif (is_a($controlClass, Control\Calendar::class, true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function _renderItemsForModel(): void
{
foreach ($this->model as $id => $row) {
$title = $row->getTitle();
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $id));
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $id));
$this->_tItem->set('title', $title || is_numeric($title) ? (string) $title : '');
// add item to template
$this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml());
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function _addCallBackRow($row, $key = null): void
{
if ($this->model !== null) {
$res = ($this->renderRowFunction)($row);
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $row->getId()));
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $row->getId()));
} else {
$res = ($this->renderRowFunction)($row, $key); // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/10283#issuecomment-1850438891
$this->_tItem->set('value', (string) $res['value']); // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/10283
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/DropdownCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public function getNewValues($id): array
if ($this->renderRowFunction) {
$res = ($this->renderRowFunction)($row);
$values[] = [
'value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getField($model->idField), $row->getId()),
'value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getIdField(), $row->getId()),
'text' => $res['title'],
'name' => $res['title'],
];
} else {
$values[] = [
'value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getField($model->idField), $row->getId()),
'value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getIdField(), $row->getId()),
'text' => $row->get($model->titleField),
'name' => $row->get($model->titleField),
];
Expand All @@ -113,7 +113,7 @@ public function getNewValues($id): array
private function getJsValues(array $values, $value): array
{
$model = $this->cascadeFrom->model->ref($this->reference);
$valueStr = $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getField($model->idField), $value);
$valueStr = $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getIdField(), $value);

foreach ($values as $k => $v) {
if ($v['value'] === $valueStr) {
Expand Down
9 changes: 6 additions & 3 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ public function renderRow(Model $row): array
*/
protected function defaultRenderRow(Model $row)
{
$idField = $this->idField ?? $row->idField;
$titleField = $this->titleField ?? $row->titleField;
$idField = $this->idField
?? $row->idField;
$titleField = $this->titleField
?? $row->titleField;

return [
'value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($row->getField($idField), $row->get($idField)),
Expand Down Expand Up @@ -391,7 +393,8 @@ protected function renderView(): void
$this->initDropdown($chain);

if ($this->entityField !== null && $this->entityField->get() !== null) {
$idField = $this->idField ?? $this->model->idField;
$idField = $this->idField
?? $this->model->idField;

$this->model = $this->model->loadBy($idField, $this->entityField->get());

Expand Down
6 changes: 2 additions & 4 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,7 @@ private function getExpressionValues(Model $entity): array
return [];
}

$dummyModel = new Model($entity->getModel()->getPersistence(), ['table' => $entity->table]);
$dummyModel->removeField('id');
$dummyModel->idField = $entity->idField;
$dummyModel = new Model($entity->getModel()->getPersistence(), ['table' => $entity->table, 'idField' => false]);

$createExprFromValueFx = static function ($v) use ($dummyModel): Persistence\Sql\Expression {
if (is_int($v)) {
Expand All @@ -801,7 +799,7 @@ private function getExpressionValues(Model $entity): array
$dummyModel->addExpression($field->shortName, [
'expr' => isset($dummyFields[$field->shortName])
? $dummyFields[$field->shortName]->expr
: ($field->shortName === $dummyModel->idField
: ($field->shortName === $entity->idField
? '99000'
: $createExprFromValueFx($entity->getModel()->getPersistence()->typecastSaveField($field, $field->get($entity)))),
'type' => $field->type,
Expand Down
2 changes: 1 addition & 1 deletion src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private function explodeSelectionValue(string $value): array
{
$res = [];
foreach ($value === '' ? [] : explode(',', $value) as $v) {
$res[] = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->model->getField($this->model->idField), $v);
$res[] = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->model->getIdField(), $v);
}

return $res;
Expand Down
2 changes: 1 addition & 1 deletion src/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function renderRow(): void
if ($this->tRow->hasTag('_title')) {
$this->tRow->set('_title', $this->currentRow->getTitle());
}
$idStr = $this->getApp()->uiPersistence->typecastAttributeSaveField($this->currentRow->getField($this->currentRow->idField), $this->currentRow->getId());
$idStr = $this->getApp()->uiPersistence->typecastAttributeSaveField($this->currentRow->getIdField(), $this->currentRow->getId());
if ($this->tRow->hasTag('_href')) {
$this->tRow->set('_href', $this->url(['id' => $idStr]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function renderRow(): void

// render row and add to body
$this->tRow->dangerouslySetHtml($htmlTags);
$this->tRow->set('dataId', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getField($this->model->idField), $this->currentRow->getId()));
$this->tRow->set('dataId', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $this->currentRow->getId()));
$this->template->dangerouslyAppendHtml('Body', $this->tRow->renderToHtml());
$this->tRow->del(array_keys($htmlTags));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected function cloneColumnView(View $view, Model $row, string $nameSuffix):

$view = $cloneViewWithAddLaterFx($view);
$view->shortName = 'c' . $this->nameInTableCache . '_' . $nameSuffix . '_r'
. $this->getApp()->uiPersistence->typecastAttributeSaveField($row->getField($row->idField), $row->getId());
. $this->getApp()->uiPersistence->typecastAttributeSaveField($row->getIdField(), $row->getId());
$view->name = \Closure::bind(static fn (Table $table) => $view->_shorten($table->name, $view->shortName, null), null, Table::class)($this->table);

return $view;
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Column/ActionButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function addModal($button, $defaults, \Closure $callback, $owner = null,
$modal = Modal::addTo($owner, $defaults);

$modal->set(function (View $t) use ($callback) {
$id = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->table->model->getField($this->table->model->idField), $t->stickyGet($this->name));
$id = $this->getApp()->uiPersistence->typecastAttributeLoadField($this->table->model->getIdField(), $t->stickyGet($this->name));
$callback($t, $id);
});

Expand Down
2 changes: 1 addition & 1 deletion src/Table/Column/DragHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function init(): void
*/
public function onReorder(\Closure $fx): void
{
$this->cb->onReorder($fx, $this->table->model->getField($this->table->model->idField));
$this->cb->onReorder($fx, $this->table->model->getIdField());
}

#[\Override]
Expand Down
2 changes: 1 addition & 1 deletion src/UserAction/CommonExecutorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected function executeModelActionLoad(Model\UserAction $action): Model\UserA
$model = $action->getModel();

$id = $this->getApp()->uiPersistence->typecastAttributeLoadField(
$model->getField($model->idField),
$model->getIdField(),
$this->stickyGet($this->name)
);

Expand Down
2 changes: 1 addition & 1 deletion src/UserAction/ConfirmationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function doConfirmation(View $modal): void
$this->loader->jsLoad(
[
'step' => 'execute',
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getIdField(), $this->action->getEntity()->getId()),
],
['method' => 'POST']
),
Expand Down
2 changes: 1 addition & 1 deletion src/UserAction/JsCallbackExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function executeModelAction(): void
$this->invokeFxWithUrlArgs(function () { // backup/restore $this->args mutated in https://github.com/atk4/ui/blob/8926412a31/src/JsCallback.php#L71
$this->set(function (Jquery $j, ...$values) {
$id = $this->getApp()->uiPersistence->typecastAttributeLoadField(
$this->action->getModel()->getField($this->action->getModel()->idField),
$this->action->getModel()->getIdField(),
$this->getApp()->tryGetRequestPostParam($this->name)
);
if ($id && $this->action->appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD) {
Expand Down
Loading

0 comments on commit 6c609cf

Please sign in to comment.