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.
Related
I am new to laravel and using laravel 8. I have following tables and I am not able to understand how to establish eloquent relation between them.
Table - Fabrics
id
user_id
name
1
1
fabric 1
2
1
fabric 2
Table - Mesh
id
name
1
mesh 1
2
mesh 2
Table - Designer
id
user_id
mesh_id
fabric_id
name
1
1
1
1
designer 1
2
1
1
2
designer 2
I have done all the basic relations. Each designer has one mesh and one fabric. there is a unique combination of fabric_id and mesh_id in designer table meaning one fabric and one mesh can not have more than one row. now I want the list of fabrics with the sublist of meshes with which designers are created. basically from the designers table instead of
with(designers) I want with(meshes). Please help. Thank you in advance.
Looks like a many to many relationship problem. See this documentation
There is a good example on many to many relationship.
You have a many to many relationship between fabrics and mesh table with designer table being the intermediate table linking both tables together.
Therefore you should already have meshes relationship in your fabrics table using belongsToMany(Mesh::class) and have fabrics relationship in your mesh table using belongToMany(Fabrics::class).
You can retrieve all the fabrics and its related meshes through its relationship.
$fabrics = Fabrics::get()
foreach($fabrics as $fabric) {
$fabric->meshes
}
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
I'm developing a site like "yellow pages" with more complex DB than I had ever experienced before.
For example:
This DB has 3 tables:
1. organisations – with some general information.
2. services - with list of all services that can be provided.
3. relation table.
Organisations table structure:
id name city phone etc.
1 CompanyName Farewell 987-65-43
Services table structure:
id ServiceType
1 purchase
-----
2 sale
-----
3 exchange
etc..
you get the point
And relation table structure:
id OrganisationID ServId
1 1 2
----
2 1 3
----
3 1 6
----
4 2 3
In website output I need a list of all organisations with all the services they provide.
I can't fully understand (even after reading Linking Models Together section in the CookBook) how I should combine all this tables using Cake's syntax and conventions.
I'll be glad if anyone can provide me a Cake's model/model's (how many I need?) example with ability to retrieve this data.
I can't comment on your question so I'll ask here...
Organisations can have many services and services can have many organisations? Is it HABTM relationship? (Just checking...)
If it is, I'll then edit this and try to give you the answer. :)
EDIT:
TABLES
1. table: organisations
2. table: services
3. table: organisations_services (sorted alphabetically)
1. table: id, name, city...
2. table: id, service_type
3. table: id, organisation_id, service_id (singular, sorted alphabetically)
MODELS
Organisation - inside your organisation model you must have:
var $hasAndBelongsToMany = array('Service');
Service - inside your service model you must have:
var $hasAndBelongsToMany = array('Organisation');
And that's all. Everything else leave to the beautiful Cake. :) If you don't understand something, feel free to ask and here you can get more information.
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.
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