How to create dynamic tables in MySQL - php

I am trying to creating a system where all new tables will be dynamically created (or objects inside a predefined databases.)
Here is an over view of the design, I would have a predefined table for
Objects (ie. object 1, 2, 3 ....n)
Fields (related to Object) field "ABC" is related to object (field 1 is linked to object 2....)
Data (ie. value for each field) so row 10 in this table is the value for field 1)
For example
In the "objects" table I have a rows to look like this:
object_id object_name
1 accounts
2 users
...
...
...
In the fields table I will have the following rows:
field_id field_label object_id
1 Account Name 1
2 Specialty 1
3 DBA 1
4 First Name 2
5 Last Name 2
...
...
...
Finally, the data will be captured in the Data table and it will look something like this:
data_id value field_id object_id
1 Mike 4 2
2 Signs/Graphics 2 1
...
...
...
This design will allow me to dynamically create a new object, create fields inside this object, and finally capture data for each field. Everything will be done without having to create/alter tables at all.
Problem
The problem that I am having is, how would I generate a unique identified for each object? ( I need to be able to identified one row inside a dynamic object)
For instance, if I want to create 100k accounts "unique accounts" inside the accounts object. How can I automatically assign a unique ID for each row?
I should be able to create a new field and call it "account ID" in the field object and relate it to object_id 1. Then automatically populate an auto increment value inside that field. Since the field "account ID" is the primary identifier it should unique for each object.
I believe that I complete understand the design, but not sure how to auto generate unique identifier. The unique identifier will later be used to capture the data before storing it in the data object. The data object will then be something like this
data_id value field_id object_id object_unique_id
1 Mike 4 2 1
2 Signs/Graphics 2 1 1
3 Jack 4 2 2
3 Jay 4 2 3
...
...
...
row 1 of the data object will point to the field_id, object_id, and object_unique_id which represent the exact account that I the "First Name" field reference to.
I am using MySQL as DBMS and PHP as Scripting Language to accomplish this.
Any help will be greatly appreciated

Related

laravel: Can not create relation in model using foreign key and non-primary key

We are getting an issue in creating the relationship inside the laravel model.
Requirement: We want to create a relation in such a way that we have three tables
block_primary_table
block_id
block_name
1
XYZ
2
ABC
block_translation_table
id
language_id
block_id
block_name
1
1
1
XYZ-English
1
2
1
XYZ-Hindi
1
1
2
ABC-English
1
2
2
ABC-Hindi
and third table for a user, where we capture the user area
user_area
user_id
user_name
district
block
1
ABC-user
1
1
and we have a form for a module where we have a district and block field the block field is dependent on the district field, so while creating a new entry the block is loaded into the dropdown depending upon district value so that we can show the value easily because it will be handled by an ajax.
but during the edit operation, suppose we are editing user_id 1 from user_area that time existing value needs to come into the form, but here the issue like other fields value comes correctly, but for the block, during edit operation, the value comes up to field using entity, that is defined in the model, so in that relationship we have
//relationship with block translation table
public function blockRelation() {
return $this->belongsTo(blockTranslation::class, 'block','block_id');
}
so, we are expecting that it will return the value from the block translation table by mapping the block(foreign key of user_area) with block_id(non-primary key of block_translation_table) of block_translation_table.
we are column name in relation still it is mapping block column of user_area table with id column of block_translation_table.
So please provide the solution for the issue, like how can we create the relationship in the laravel model for foreign key and non-primary key.

Symfony3 with checkboxes retrieved from database tables

I'm trying to get a good Symfony3 solution for a Form containing checkboxes. The total number of checkboxes are retrieved from one table (the CategoryList table) and in another table I've stored which Categories for a certain domain are selected.
Table: Domain
ID DomainName
1 TestDomain1
2 TestDomain2
3 TestDomain3
Table: CategoryList
ID CategoryName
1 Unknown
2 Cat-1
3 Cat-2
Table: DomainCategory
ID Domain_ID DomainCategoryList_ID
1 1 1
2 1 2
3 1 3
4 2 2
5 3 1
The DomainCategory table contains the relationship between the Domains and the categories to which the Domains belong to.
Domains --> DomainCategory <--> CategoryList
How can I create a Symfony3 form for this?
I tried to use the EntityType::class and passing the CategoryList object to it, then I get indeed all the Categories as checkboxes but I cannot specify which checkbox needs to be checked/ticked depending on the DomainCategory table.
Any suggestions on how to solve this? Or any good reference to an example implementation. I was not able to find it after spending a week solving this problem.

MySQL search in field (or other solutions)

I have a table with products that fall under specific categories, but the products within each category can contain multiple meta data tracking field
Table: products
id name category metadata
1 something 1 blue,red,purple
2 something else 2 left,right,middle
I have been trying to contemplate the best method to have a single product table but can't seem to squeeze the metadata in conveniently. for now I have created a table with all the metadata and fields for tracking the related category (the sequence is so i can order them withing a dropdown etc..)
Updated table: products
id name category metadata
1 something 1 1,2,3
2 something else 2 4,5,6
Table: metadata
id category sequence option
1 1 1 blue
2 1 2 red
3 1 3 purple
4 2 1 left
5 2 2 right
6 2 3 middle
If this format makes sense .. I am trying to generate a query that will search for values in my product table and grab each and all of the related meta values. The issue I am having is trying to find a unique value in the products field. if I do a MySQL search for LIKE(%1%) I will get matches for 1, 11, 21, 31 etc ... I thought of adding a leading and trailing comma to the field by default and then search for ",1," which would be unique .. but there has to be a better way ...
Any recommendations (regarding format or query)?
It's not an ideal design to have comma-separated values within a single database field. Aside from the problem you mentioned (difficult to search), your queries will be less efficient, as the DB won't be able to use indices for the lookup.
I'd recommend making a separate table products_metadata with a many-to-one relationship to the products table. Have the *metadata_id*, and the *product_id*, which is a foreign key linking back to the products table. That will make your job much easier.
You want to add another table, which links products to their metadata. It will have two columns: productid and metadataid which refer to the relevant entries in the products and metadata tables respectively. Then you no longer keep metadata in the products table, but JOIN them together as required.

How to Store Multiple Options selected by User in a Table

So I want my users to be able to restrict who may contact them.
There are several factors they should be able to filter, including Age (e.g. Must be between 18 - 29), income (must earn between $25,000 - $60,000), what they're looking for (e.g. Friendship, Hang out, etc.), what drugs they do (Marijuana, Meth, Cocaine, etc), etc.
The problem is, I want them to be able to select and store multiple choices for some of the criteria (e.g. drugs), but I do not know how I should store that in the DB or how I should structure the table to best accomplish that.
For example, how would I store a user's row that for "drugs" chose "Marijuana", "Cocaine", and "Heroin" within this context? Would I simply store those as comma-separated values in the "Drugs" column? Or should I do it in a completely different way?
What would be the best way to do this (considering I will obviously have to retrieve and check this information every time a user wants to contact another user) and why?
No, don't store the values in CSV format in the database. Instead create a join table called user_drug and store one row for each user/drug combination:
user
id name income
1 Foo 10000
2 Bar 20000
3 Baz 30000
drug
id name
1 Marijuana
2 Cocaine
3 Heroin
user_drug
user_id drug_id
1 1
1 2
2 1
2 3
3 3
A DB column (at least theorethically) should NOT hold multiple values. Unfortunately, there are some programmers that store multiple values in a single column (values separated by comma for examples) - those programmers (in most cases) destroy the concept of DB and SQL.
I suggest you to read about Database Normalization to get a start in organizing your tables. And, do your best to achieve the Codd's Third Normal Form
You can try with this:
criterium
------------
user_id type value
1 AGE_MIN 18
1 AGE_MAX 29
1 INCOME_MIN 25000
1 INCOME_MAX 60000
1 DRUGS Marijuana
1 DRUGS Meth

How to store an array of an inventory in php/mysql?

The end user has an inventory such as:
dogs:0, cats:3, dragons:20 etc you get the point.
So for a 30 inventory string array, what is the best way to store the values in mysql
and then use them with php? do i need to use json for this? because i would like
to update for instance a user "buying" another dragon and update the record occasionally.
Just use a seperate table for this, which has a reference to the object (if an object table exist), the player it belongs to and the number of items. No need to store a json string in a table.
Table Objects
-----------------
id | Object name
Table Inventory
----------------------------------
id | player_id | object_id | count
Store in a table containing Id, Name, Quantity.
You need JSON to communicate with php without reloading the page (AJAX), but it can be generated dynamically.

Categories