DB Structure for saving form data for dynamically created forms - mysql

I am considering a use case wherein every user of the application can create N number of forms with n different form fields of different data types. Each user can then receive form data in their respective forms. This data can then be updated, edited, searched, sorted etc.
I wonder, how do I design the DB architecture for this in MySQL.
My initial approach was to save form structure in serialized form in one table with form_id as primary key. Another table, data shall hold the records with columns: form_id, record_id, order, value. Here, order will be number denoting the position of the field in the form structure. value column shall have the value for that field. This approach caused me to have 10 rows (for a form with 10 fields) for 1 set of records. Also, I don't think such a query can be written to search records for a particular form.
I did think of using mongodb for this use case, wherein form structure shall be stored in an array and within that array, all records for that particulsr form shall be stored. I have never ever used mongodb, but I guess mongodb has some sort of restriction on size, if I store documents within a document. So, what is the best way to do this using MySQL (or any other DB perfect for such a use case)

Each mongoDB document is restricted to 16mb in size. If you needed each document to be bigger then you would need to adapt your design appropriately. MongoDb does also include GridFS which allows you to break larger files into chunks.
In your example, you could store the form fields as an array in mongoDB but you could also store them as an embedded document. Whichever way you choose to do it, your schema design in mongoDB needs to match your data access patterns.
It's easy to add new key:value pairs to mongoDB documents through your application. Pseudocode would be:
if user adds form field
db.collection.update({'id':1},{$addToSet {'newField':'value'}})
I know your question was about MySQL structure but I thought it worth showing how it could work in mongoDB (obviously would need to be adapted to your chosen language).

Related

Laravel Nested views with logic

please I need help as I can't figure out what the best approach to my problem is.
I will try to make the long story short:
I have an application which has many subapplications laveraging from the same code and is entirely database driven. Lets imagine that you have a record which doesn't use the typical horizontal database approach but rather vertical, meaning 1 record with 40 fields doesn't have 1 row with 40 columns in database but 40 rows which have the same "record id" and each row represents a different field where each row's other columns specify other atributes of that field like order,section or field id and field type.
When I say field type I don't mean just regular field types like checkbox,radio,text etc. but there are also custom fields which have complex logic.
now, each field type has it's own template(view) which is included in a loop when the whole record is created on the view. So when I want to update a record updadeEditController takes the record based on the record id from the database which will grab those e.g. 40 fields meaning 40 rows and sends it to the updateEdit view. In this view it is looping through those 40 rows and calls/includes views for each field type.
#foreach ($fields as $field)
#if($field->intSectionId == $section->id)
#include('field.edit.' . $field->strFieldType
#endif
#endforeach
as I mentioned some of these fields have some complex logic including queries etc which should run and render data on each field's view and I don't want to put logic in each field type view in blade which is considered a bad practice and it should be.(but it would really help in this scenario :) )
my question is what is the best approach to bind a logic to each subview which is generated dynamically based on field set. As I mentioned at the beginning this application has many subapplications which are basically using the same code and the number of applications may grow and also field sets may be changed for each subapplication going further so it is important for me to keep each fields logic somewhere else than in the controller itself, but rather have the logic for each field view somehow tied to those views.
thanks.

Lookup Fields MS Access

I am somewhat new to MS Access and I have inherited an application with a table that uses this Lookup feature to replace a code with a value from a query to another table.
When I first used this table and exported it to Excel for analysis, I somehow got the base ID number (or whatever it would be called) rather than the translated lookup value. Now, when I do this, I get the translated text. The biggest problem is that while the base value is unique, the translated values are not, so I cannot use them for the work I am doing.
Can someone explain how to get the underlying ID value rather than the lookup value? Is there some setting I can use or some way to reference the field upon which the lookup is based. When I query the ID field, I get the lookup value. I know that the first time I did this, the spreadsheet contained the ID number not the text.
For now, I created a copy of the table and removed the lookup information from this copy, but I know I did not run into this when I did this the first time.
Thanks.
When you export to Excel, leave Export data with formatting and layout unchecked. This will create a spreadsheet with raw data values in Lookup fields.
Export settings image

Use a another table's column's value as the name of a table in a join statement

First, I'll ask the question:
Is it possible to make a value's type 'dynamic' by creating different tables with different value types and use a simple join query to fetch the correct table based on the type specified in a column?
Here is the context:
I'm building a sort of page builder where I want the "modules" to be dynamic in the sense that I can create new modules without having to modify the database in any way.
I envision 4 entities:
A field, which represents a single value of a specific type, for example a text field or a checkbox;
An entry type, which represents a group of fields to be used for an entry. For example, Article = Title + Content + Image.
An entry, which is the content defined by the corresponding entry type.
A value, which contains the data for one field for one entry, defined by the field's value type.
For the 'value' entity, I was planning on using multiple tables for each "type of value". "value_text", "value_integer", "value_boolean", etc.
What I wanted to do (and I think it's an anti-pattern in relational database) was to grab a value in the fields table to select the proper "value" table.
Here is an image of the structure I envisioned:
(imgur) UML Diagram of DB
So I'm looking for a way to make a single query to fetch the correct value for each field of a specific entry (by ID).
This means I have to access the assigned entry_type, fetch all the fields related to that entry_type, then fetch the value of each of those fields from the value tables based on the column 'value_type' of the fields table.
If this is possible, how can I achieve this? If it's not possible, what would be the best way to tackle this problem:
Make a first query to retrieve the fields, then a query for each field to grab the correct value? This makes a lot of queries to get the values of a single entry...
Modify the structure of the database to a more efficient set of relations, taking into account everything is dynamic...
Get rid of the idea of using mutiple "value types" and stick to using a more common everything-is-serialized-text approach.
Also, as this is one of my first questions on StackOverflow, please let me know if I asked incorrectly and how I should formulate this in the future.
Thanks for all of you who even take the time to read this :)

Use query's hidden columns/fields in MS Access report

I have a query which joins a few tables. The query asks for a parameter when it is run. Then it displays a few columns/fields from its results, the other fields (like, the entered parameter and other fields that are common to all rows) are hidden. I'm trying to create my first Access report based on this query and I'd like to use some of these hidden fields as a record source for text-boxes/labels but I've been unable to do so. Those fields don't appear in the list of possible record sources.
I've tried the Add Existing Fields button and selecting the field I want from Fields in related tables but when the bound textbox shows up in the report with an error like Invalid Control Property. No Such Field in the Field List which I think means the fields are still invisible to the report. Would appreciate any help getting this done.
The answers is to edit the query and include the missing columns so that they are available to the report.
For non-selected query columns, the use of the word "hidden" is misleading. The way that SQL works--even beyond SQL in the overall concept of datasets--is that only subsets of data are requested in a query. If certain columns are not selected in a query, they are plainly and simply NOT available in the resultant dataset.
It may be possible to re-aquire the excluded columns with another query, but that still does not imply that missing columns were just "hidden". In the case of a subsequent query to get different columns of data, the database engine must re-analyze the new query, re-read the data, then compile the data from the new set of columns to be returned in a completely new subset of data. It is a complete round-trip process that is much more involved than just un-hiding the data.
Forms and reports are completely different types of objects that can mark fields as "hidden", but a query is not the same despite being displayed in a datasheet. Objects like Access Forms and Recordset objects in (VBA) code are designed to hold subsets of queried data, so they are able to temporarily hide (or ignore) or un-hide certain fields without re-querying the database. (Technically an Access Form has its own Recordset object for holding and managing queried data.)
Even though Access is "self contained" with both database and user-interface elements, it is still primarily a standard RDBMS (Relational Database Management System). The principle idea in efficient data handling of an RDBMS is to get only what you need for the current operation. Consider that SQL can be used to query data from a remote server. Even Access can get data from and update data on SQL Server, for example. If data fields were only "hidden" when not included in a query, that would imply that the entire database (or at least an entire table) would be passed back and forth, and that the only thing hindering getting at any column would be "making it visible" as though it is actually immediately available. That would be terribly inefficient both for memory and for remote communications.

MySQL: Storing a value from a field that may show up variable times on a form

I'm creating a form in CodeIgniter that has a paired value- Companies, and CEOs. The idea is, while there is only one Company field with one CEO field below initially on the form, there is a button for me to create more pairs of Company-CEO fields. I have an idea for how to make that appear on the frontend, but I'm not quite sure which is the best way to store it on the backend.
I think in order to combine each Company and CEO field into one value for storing in the MySQL database, I could simply use CONCAT. However, I'm not sure if I should even bother creating such a value. In any case, how would I handle the fact that there are a variable number of Companies (and thus CEOs) for each record? Would I have to store Company and CEO within another value? How could such an array or tuple be handled in MySQL?
Point 1: Do not combine the values. It will give you headaches in code splitting and combining. Make two columns, one for company, one for CEO.
Point 2: In general, when using a database, any time you have a list it gets its own table.
The various JS libraries out there let you handle this with a grid.