i am trying to map a database field with a json list to an object on an edit form.
All data comes from a single mysql table "customers".
The json list lives in the column named "email" and is a simple list of email addresses associated to a customer entry.
(I cannot use a fixed number of columns for email addresses in the database because i want the list length to be dynamic.
I can also not use a separate mysql table and cross-references because i need the customerdatabase to be "flat" like a spreadsheet.)
If i just use an "Input" object of type "Text" i would require my webui users to write valid json (works but not ideal).
Using a "Textarea" object and transforming the json array to a newline separated list before-edit and back ?before-save? would do the job (i'm not focusing on sanitizing the input here

For another approach I managed to use a "Select" object to display all addresses that are already in the database (the selected entry could represent a primary address, not important).
My difficulty with that approach is to create buttons to add/delete/edit list items.
My favourite solution would be to use a subform with data from a temporary table that i generate from the json list.
In mariadb 10.6.0 a json function "JSON_TABLE" was added that makes generating that temporary table in PHP quite easy.
For the sake of testing with mariadb 10.5.x i'm using the following PHP in BE (which obviously does not support a dynamic length json list but limits it to 10 items):
Code: Select all
$tmp = nuTT();
$s = "CREATE TABLE $tmp
SELECT
JSON_UNQUOTE(JSON_EXTRACT(email, CONCAT('$[', idx, ']'))) AS email
FROM customers
JOIN (
SELECT 0 AS idx UNION
SELECT 1 AS idx UNION
SELECT 2 AS idx UNION
SELECT 3 AS idx UNION
SELECT 4 AS idx UNION
SELECT 5 AS idx UNION
SELECT 6 AS idx UNION
SELECT 7 AS idx UNION
SELECT 8 AS idx UNION
SELECT 9
) AS indexes
WHERE id = #USER_ID# AND
JSON_EXTRACT(email, CONCAT('$[', idx, ']')) IS NOT NULL;"
nuRunQuery($s);
The "saving-procedure" then will somehow have to translate the list back to a json array and insert it into the corresponding mysql column.
Does anyone maybe have a better idea how to do this?
Or can give me some hints on how to make any of the approaches work (preferably the last one

Thanks a lot,
Julian