I've been pondering the best way to approach the structure of this database. What I have so far is:
table category
cat_id (PK)
title
table sub_cat
sc_id (PK)
cat_id (FK)
title
This is all fine since I could use it as follows:
Car -> Mazda
computer hardware -> CPU
clothing -> Male :: Pants
Car Parts -> Mazda :: Spoilers
...
The problem is how could I best structure the database to further utilize the best of each category. The cars data will require a lot of fields suitable for cars alone which another category may require something completely different.
It should be said there will be a lot of categories.
Possible is to create a table 'Details', built something like this
= ItemId === DetailId === DetailValue =
| Somecar1 | 1 | Blue |
| Somecar2 | 2 | Four |
| Somecar2 | 1 | Pink |
| Somecar2 | 2 | Two |
=======================================
Where DetailId refers to something like this, detailreference;
= CategoryId === DetailId === DetailString =
| 1 | 1 | Color |
| 1 | 2 | Doors |
============================================
If you want to get the info of Somecar1, you should query the Details table for the data, then query detailreference to get the type of detail stored inside of a certain number, for a certain category.
Related
Ok, I hope I can make this question as clear as possible.
I have the following table:
table: phones
phone_id | name | ... ... ... | manufacturer_code
-----------------------------------------------------
1 | samsung | | 001
2 | apple | | 002
3 | htc | | 003
and so on...
I have A LOT of columns (... ... ... means at least 40 different columns like width, height, depth, color, has_bluetooth, ... stuff like that).
Now I want to do this differently, but I have NO IDEA how to start.
How I THINK to do it? (correct me if i'm wrong!)
1) Store the field names in a different table
table: phone_fields
field_id | name
--------------------------------
1 | name
2 | width
3 | weight
4 | depth
... | ...
41 | manufacturer_code
2) Connect the fields with the data for each phone in another table
table phones
row_id | phone_id | field_id | value
------------------------------------------------------
1 | 1 | 1 | samsung
2 | 1 | 2 | 10,50 cm
3 | 1 | 3 | 1 kg
... (and so on).
I want this to be searchable/filterable, I have a filter now which filters on brand, color, pricerange, ... and this works fine.
How should you guys do it? Or does anyone have a useful link/tutorial about this? English is not my native language so I don't know exactly how to search for it.
Thanks in advance!
Edit: why am I doing this? I want to be able to add extra fields through my admin-panel if necessary (and not via phpmyadmin or something like that).
If your request always returns the entire set of fields, then leave it in one table
If not, then you can leave only the most important fields in one table, and in the other table place the remaining attributes and link it via foreign key
I'm trying to wrap my head around designing my database which will store one or more preferences for many categories for each user. So in other words, each user can select one or more options from the Colors category, one or more options from the Shapes category, and so on.
My initial thought was to first have a User table with generic user information. Next, there would be a table to store all the different categories as so:
CATEGORY_ID | CATEGORY_VALUE
--------------------------------
1 | Colors
2 | Shapes
3 | Sizes
I'd separate each Category into it's own table (Colors for example):
OPTION_ID | OPTION_VALUE
------------------------------
1 | Red
2 | Blue
3 | Green
Finally, I would have a User Preferences table:
USER_ID | CATEGORY_ID | OPTION_ID
----------------------------------------
1 | 1 | 2
1 | 1 | 3
1 | 3 | 2
2 | 1 | 3
Am I on the right track here or is there a better/more efficient way to designing this. I will be setting up a search results page which will allow visitors to filter through these different categories.
Thanks!
I have task that would be quite simple using regular SQL query but the project is built using doctrine and I am looking for an optimal solution. Maybe someone could advise what would be a good way to approach this.
I have a quite complicated db structure but the simplified version of objects in question look like this:
| Category | | Product | | ProductOption |
------------ --------------- --------------------
| id | | id | | id |
| name | | category_id | | product_id |
------------ | name | | some_data |
--------------- --------------------
Product Option and Product have 1 to 1 connection. But options are created per category (I get 1 entity per category, but need to replicate that entity for every product and store that as 1 to 1 since at some point those options will need to be edited individually. Now there are many ways to do that (the dirty way) , but I would like some advice on how to do that in the most optimal way.
I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')
I'm attempting to add Breadcrumbs to my website using a MySQL table, and I'm having difficulty at the moment.
I have a table named 'includes' created that stores information about the category, page, subpage, the title, and the ref (url) of the page. Category, Page, and Subpage are all php parameters passed from the page the user is on
My table is laid out like this:
|----------------------------------------------------------------|
| ID | Category | Page | Subpage | Title | Ref |
|----------------------------------------------------------------|
| 0 | | | | Home | ... |
| 1 | Software | | | Software | ... |
| 2 | Software | Desktop | | Desktop Software | ... |
| 3 | Software | Mobile | | Mobile Software | ... |
| 4 | Software | Desktop | Blah | Blah Blah | ... |
| ...
|----------------------------------------------------------------|
What I'm trying to do is make a query that will return only the required steps back to home for the breadcrumbs.
In other words, if the user is on "example.com/software/desktop/blah", the query will return rows 0,1,2, and 4. Or if I was on /software/mobile, it would only return rows 0,1, and 3.
My current attempts have been things like the following:
SELECT * FROM `includes` WHERE
`category` IS NULL AND `page` IS NULL AND `subpage` IS NULL OR
`category`='$category' AND `page` IS NULL AND `subpage` IS NULL OR
`category`='$category' AND `page`='$page' AND `subpage` IS NULL OR
`category`='$category' AND `page`='$page' AND `subpage`='$subpage'
Which not only don't work, but also seem more complex than it should have to be.
I'm probably overcomplicating this, or possibly just doing an entirely wrong method, which is why I've turned here.
Does anyone have a possible solution to this? Should I be looking at a more complex query? (admittedly, SQL is not my forte) Or should I be looking at a new SQL table, or possibly an entirely different method?
What you have is a hierarchical structure. The data is set up with parent-child relationships. There is a good description on how to work with hierarchical data here: http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/
You can make a self relation table like this
id | parent_it | title | Ref
1 | 0 | Home | ...
2 | 1 | Software | ...
3 | 2 | Desktop | ...
4 | 2 | Mobile | ...
5 | 3 | Blah | ...
So your query should get the last element
SELECT * FROM includes WHERE
tilte = 'Blah'
And then get the parent ID title and so on , like this the table structure will be better from my point of view & experience
OR
Generate your query based on the values you get , with simple loop count the arguments and based on that generate the query string then execute it
I hope this can help :)