Skip to content

Prefix/test also all ID fields #1595

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

Merged
merged 27 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
56e200a
Better title field prefix
mvorisek Feb 26, 2021
1c97747
reformat hasXxx to multiline
mvorisek Feb 22, 2021
01969b5
Prefix/test also all ID fields
mvorisek Feb 26, 2021
558eac9
fix our_field with hasOne
mvorisek Feb 26, 2021
54e0cdd
fix prefixing gen
mvorisek Feb 26, 2021
a1cde18
improved seed update
mvorisek Feb 26, 2021
abb66af
checked fixes
mvorisek Feb 26, 2021
8e2dbf5
unchecked fixes
mvorisek Feb 26, 2021
e744f95
fix hardcored id name
mvorisek Mar 1, 2021
7ff9859
DEBUG with fixed atk4/data
mvorisek Mar 3, 2021
5e47700
Revert "DEBUG with fixed atk4/data"
mvorisek Mar 3, 2021
bc93d11
DEBUG simpler regex replace - field is prefixed twice somewhere
mvorisek Mar 3, 2021
29934c4
fix
DarkSide666 Mar 3, 2021
aed4b06
revert fix, not needed
DarkSide666 Mar 3, 2021
f185d5c
Revert "DEBUG simpler regex replace - field is prefixed twice somewhere"
mvorisek Mar 3, 2021
abf0dc1
even stricter regex
mvorisek Mar 3, 2021
ebd2acd
fix id in user confirmation executor
DarkSide666 Mar 3, 2021
f447154
cs fix
mvorisek Mar 3, 2021
843b624
fix more hardcoded id fields
DarkSide666 Mar 3, 2021
351e5f8
fix test
DarkSide666 Mar 3, 2021
586d46f
undo
DarkSide666 Mar 3, 2021
72446b7
c0 is used in crud
DarkSide666 Mar 3, 2021
0bf306c
simpler always false condition
mvorisek Mar 3, 2021
ceef157
Revert "simpler always false condition"
mvorisek Mar 3, 2021
66042ba
test
DarkSide666 Mar 3, 2021
2797513
just get rid of it - looks like its fixed now too
DarkSide666 Mar 3, 2021
4f73b0c
remove all
DarkSide666 Mar 3, 2021
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
12 changes: 7 additions & 5 deletions demos/_demo-data/create-sqlite-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ class ImportModelWithPrefixedFields extends Model
{
private function prefixFieldName(string $fieldName, bool $forActualName = false): string
{
if ($fieldName === 'id') {
return $fieldName;
}

return 'atk_' . ($forActualName ? 'a' : '') . 'fp_' . $this->table . '__' . $fieldName;
}

public function addField($name, $seed = []): \Atk4\Data\Field
{
$seed['actual'] = $this->prefixFieldName($name, true);
if ($name === 'id') {
$this->id_field = $this->prefixFieldName($name);
}

$seed = \Atk4\Core\Factory::mergeSeeds($seed, [
'actual' => $this->prefixFieldName($name, true),
]);

return parent::addField($this->prefixFieldName($name), $seed);
}
Expand Down
3 changes: 2 additions & 1 deletion demos/_includes/DemoActionsUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static function setupDemoActions(CountryLock $country): void
{
$country->addUserAction(
'callback',
['description' => 'Callback',
[
'description' => 'Callback',
'callback' => function ($model) {
return 'callback execute using country ' . $model->getTitle();
},
Expand Down
2 changes: 1 addition & 1 deletion demos/collection/lister-ipp.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
$row->iso = mb_strtolower($row->iso);
});
$lister->setModel(new Country($app->db))
->addCondition('id', -1); // no such records so model will be empty
->addCondition(Country::hinting()->fieldName()->id, -1); // no such records so model will be empty

\Atk4\Ui\View::addTo($app, ['ui' => 'clearing divider']);
\Atk4\Ui\Header::addTo($app, ['Item per page', 'subHeader' => 'Lister can display a certain amount of items']);
Expand Down
2 changes: 1 addition & 1 deletion demos/data-action/jsactions2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$country = new CountryLock($app->db);
$country->tryLoadAny();
$countryId = $country->get('id');
$countryId = $country->get($country->id_field);

// Model actions for this file are setup in DemoActionUtil.
DemoActionsUtil::setupDemoActions($country);
Expand Down
3 changes: 2 additions & 1 deletion demos/form/html-layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
$form = Form::addTo($left, [
'layout' => [
Form\Layout::class,
['defaultInputTemplate' => __DIR__ . '/templates/input.html',
[
'defaultInputTemplate' => __DIR__ . '/templates/input.html',
'defaultHint' => [\Atk4\Ui\Label::class, 'class' => ['pointing', 'below']],
],
],
Expand Down
60 changes: 42 additions & 18 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ class ModelWithPrefixedFields extends Model
private function prefixFieldName(string $fieldName, bool $forActualName = false): string
{
if ($forActualName) {
$fieldName = preg_replace('~^' . preg_quote('atk_fp_' . $this->table . '__', '~') . '~', '', $fieldName);
}

if ($fieldName === 'id') {
return $fieldName;
$fieldName = preg_replace('~^atk_fp_' . preg_quote($this->table, '~') . '__~', '', $fieldName);
}

return 'atk_' . ($forActualName ? 'a' : '') . 'fp_' . $this->table . '__' . $fieldName;
Expand All @@ -46,6 +42,19 @@ protected function createHintablePropsFromClassDoc(string $className): array
}, parent::createHintablePropsFromClassDoc($className));
}

protected function init(): void
{
if ($this->id_field === 'id') {
$this->id_field = $this->prefixFieldName($this->id_field);
}

if ($this->title_field === 'name') {
$this->title_field = $this->prefixFieldName($this->title_field);
}

parent::init();
}

public function addField($name, $seed = []): \Atk4\Data\Field
{
$seed = \Atk4\Core\Factory::mergeSeeds($seed, [
Expand Down Expand Up @@ -87,7 +96,6 @@ public function lock(): void
class Country extends ModelWithPrefixedFields
{
public $table = 'country';
public $title_field = 'atk_fp_country__name';

protected function init(): void
{
Expand Down Expand Up @@ -248,7 +256,6 @@ class Percent extends \Atk4\Data\Field
class File extends ModelWithPrefixedFields
{
public $table = 'file';
public $title_field = 'atk_fp_file__name';

protected function init(): void
{
Expand All @@ -258,10 +265,15 @@ protected function init(): void
$this->addField($this->fieldName()->type, ['caption' => 'MIME Type']);
$this->addField($this->fieldName()->is_folder, ['type' => 'boolean']);

$this->hasMany($this->fieldName()->SubFolder, ['model' => [self::class], 'their_field' => self::hinting()->fieldName()->parent_folder_id])
$this->hasMany($this->fieldName()->SubFolder, [
'model' => [self::class],
'their_field' => self::hinting()->fieldName()->parent_folder_id,
])
->addField($this->fieldName()->count, ['aggregate' => 'count', 'field' => $this->persistence->expr($this, '*')]);

$this->hasOne($this->fieldName()->parent_folder_id, ['model' => [Folder::class]])
$this->hasOne($this->fieldName()->parent_folder_id, [
'model' => [Folder::class],
])
->addTitle();
}

Expand Down Expand Up @@ -332,15 +344,20 @@ protected function init(): void
class Category extends ModelWithPrefixedFields
{
public $table = 'product_category';
public $title_field = 'atk_fp_product_category__name';

protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);

$this->hasMany($this->fieldName()->SubCategories, ['model' => [SubCategory::class], 'their_field' => SubCategory::hinting()->fieldName()->product_category_id]);
$this->hasMany($this->fieldName()->Products, ['model' => [Product::class], 'their_field' => Product::hinting()->fieldName()->product_category_id]);
$this->hasMany($this->fieldName()->SubCategories, [
'model' => [SubCategory::class],
'their_field' => SubCategory::hinting()->fieldName()->product_category_id,
]);
$this->hasMany($this->fieldName()->Products, [
'model' => [Product::class],
'their_field' => Product::hinting()->fieldName()->product_category_id,
]);
}
}

Expand All @@ -352,15 +369,19 @@ protected function init(): void
class SubCategory extends ModelWithPrefixedFields
{
public $table = 'product_sub_category';
public $title_field = 'atk_fp_product_sub_category__name';

protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);

$this->hasOne($this->fieldName()->product_category_id, ['model' => [Category::class]]);
$this->hasMany($this->fieldName()->Products, ['model' => [Product::class], 'their_field' => Product::hinting()->fieldName()->product_sub_category_id]);
$this->hasOne($this->fieldName()->product_category_id, [
'model' => [Category::class],
]);
$this->hasMany($this->fieldName()->Products, [
'model' => [Product::class],
'their_field' => Product::hinting()->fieldName()->product_sub_category_id,
]);
}
}

Expand All @@ -373,15 +394,18 @@ protected function init(): void
class Product extends ModelWithPrefixedFields
{
public $table = 'product';
public $title_field = 'atk_fp_product__name';

protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);
$this->addField($this->fieldName()->brand);
$this->hasOne($this->fieldName()->product_category_id, ['model' => [Category::class]])->addTitle();
$this->hasOne($this->fieldName()->product_sub_category_id, ['model' => [SubCategory::class]])->addTitle();
$this->hasOne($this->fieldName()->product_category_id, [
'model' => [Category::class],
])->addTitle();
$this->hasOne($this->fieldName()->product_sub_category_id, [
'model' => [SubCategory::class],
])->addTitle();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/UserAction/ConfirmationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function doConfirmation(View $modal)
$this->loader->jsload(
[
'step' => 'exec',
$this->name => $this->action->getOwner()->get('id'),
$this->name => $this->action->getOwner()->getId(),
],
['method' => 'post']
),
Expand Down
4 changes: 2 additions & 2 deletions src/UserAction/JsCallbackExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function setAction(Model\UserAction $action, $urlArgs = [])
}

$this->set(function ($j) {
// may be id is pass within $post args.
$id = $_POST['c0'] ?? $_POST[$this->action->getOwner()->id_field] ?? null;
// may be id is passed as 'id' or model->id_field within $post args.
$id = $_POST['c0'] ?? $_POST['id'] ?? $_POST[$this->action->getOwner()->id_field] ?? null;
if ($id && $this->action->appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD) {
$this->action->getOwner()->tryLoad($id);
}
Expand Down
8 changes: 4 additions & 4 deletions src/UserAction/ModalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ protected function doPreview(View $modal)
if ($prev = $this->getPreviousStep($this->step)) {
$chain = $this->loader->jsload([
'step' => $prev,
$this->name => $this->action->getOwner()->get('id'),
$this->name => $this->action->getOwner()->getId(),
], ['method' => 'post'], $this->loader->name);

$modal->js(true, $this->prevStepBtn->js()->on('click', new JsFunction([$chain])));
Expand All @@ -361,7 +361,7 @@ protected function doPreview(View $modal)
$this->loader->jsload(
[
'step' => 'final',
$this->name => $this->action->getOwner()->get('id'),
$this->name => $this->action->getOwner()->getId(),
],
['method' => 'post'],
$this->loader->name
Expand Down Expand Up @@ -552,7 +552,7 @@ protected function jsStepSubmit(string $step)
$this->loader->jsAddStoreData($this->actionData, true),
$this->loader->jsload([
'step' => $this->getNextStep($step),
$this->name => $this->action->getOwner()->get('id'),
$this->name => $this->action->getOwner()->getId(),
], ['method' => 'post'], $this->loader->name),
];
}
Expand Down Expand Up @@ -648,7 +648,7 @@ protected function jsSetPrevHandler(View $view, string $step)
if ($prev = $this->getPreviousStep($step)) {
$chain = $this->loader->jsload([
'step' => $prev,
$this->name => $this->action->getOwner()->get('id'),
$this->name => $this->action->getOwner()->getId(),
], ['method' => 'post'], $this->loader->name);

$view->js(true, $this->prevStepBtn->js()->on('click', new JsFunction([$chain])));
Expand Down