I am making an e-commerce web application. The following are the things that I have planned.
products table to contain only few columns viz. id, name, code, SKU_no.
meta_information_products table containing columns viz. id, product_id [foreign key to products table], meta_title, meta_keywords, meta_description.
measurement_product table containing columns viz. id, product_id, width, height, weight, length
And similarly other tables in relation to products.
So my questions are:
Shall I create different Model for each of the points above and
then create the One-To-One relationship with products and
related table ? Or shall I create only one Model called Product and declare all the fields in just one table product.
If I create different models for Product, what should be the name of the method to be declared for creating the One-To-One relationship, and same with Product Model.
For example, consider the following: I have created two models called Product and MetaInformationProduct and I have created relationship with both the tables. Now how do I name the method for the following:
class Product extends Model {
...
public function methodName() {
$this->belongsTo('App\Product');
}
...
}
And for MetaInformationProduct:
class MetaInformationProduct extends Model {
...
public function methodName() {
$this->hasOne('App\Product');
}
...
}
I guess Stack Overflow is not the best place to ask questions of this kind, since your questions do not necessarily have right or wrong answers. There are multiple possible ways to go about constructing your app's data structure, and ultimately it all boils down to one's personal style of coding. So almost every developer would have an original 'right answer' to your questions.
Here's what I think. Why do you need three separate tables for all that data? As far as I can see, all three tables contain data related one-to-one to a single product. That means more complicated models and relations in development, and more resources and longer execution time in production. You could avoid all that if you create one products table with the following columns: id, code, SKU_no, name, title, description, width, height, length, weight. That will simplify your models significantly and reduce the number of queries trifold.
Additionally, I think I can spot a piece of bad practice in your table structure. In your current meta_information_products table you have a column named meta_keywords. I'm guessing that that field would contain multiple keywords of a product. This negates the benefit of relational database structure and will give you headaches down the road. Instead, I would create one products table as I described in the previous paragraph, then another table titled keywords, with the following columns: id, keyword. Lastly, you'd need a relational table titled keyword_product with the following columns: id, keyword_id, product_id. This gives you the ability for one product to have multiple keywords, and for one keyword to be assigned to multiple products. It's a well known 'Many to many' relation, and you can read more about it in the Laravel's official documentation.
In general, you should create one model for one database table, except for the relational tables. So in case you do as I would, you would then need two models: Product and Keyword. For its content, it's best that you refer to the link in the previous paragraph.
Related
I am trying to use inheritance for sake of re-usability and I have a base class named 'Partner' and two sub classes 'Lawyer' and 'Accountant' as below:
abstract class Partner{
}
class Lawyer extends Partner{
}
class Accountant extends Partner{
}
It's all good with the code, but I was wondering more when it comes to database, how do I store these information in db, should I have one table partners for both entities, or I should have separate tables for each entity, based on fact that these two differs from each other based on their attributes.
If I keep it in separated tables, and I have another table 'cases' which is related to partner:
cases: case_id, subject, description,partner_id.... My question is, how do I inner join entities based on user logged in, let say if he is laywer join lawyer table or vice versa, what if I keep adding more entities in future?
How would I do it in such a way that eventhou in future I add more Entities my db queries will not have to be changed again and again.
I can't say I favor the idea of combining lawyers and acct's into one table partners. My suggestion is to keep your db entities separate. So you will have three tables (lawyer, accountant, and cases) and cases will contain foreign keys from lawyer and accountant. Your inner joins will be simple in this case. You'll have two 1:1 relationships whereas using a partner table it will have to be 1:M relationship to get both the lawyer and the accountant - each being its own record within the Partners table. Plus don't forget all the useless NULLS you'll have since lawyers and accountants have their own specific attributes..eew!
Since you are going to use Lawyers and Accountants in the same context, I think it makes sense to use the same table.
You can simply create a partners table with the common properties, and create separate accountants and lawyers tables with a 1:1 relationship. This basically means these two tables don't get their own auto-incrementing primary key, but just a partner_id that is primary and refers to the id in the partner table.
I have a MySQL database set up with a list of all my movies, which I imported from a MS Access database. One field contains the possible values for the genre of the movie, movies can have more than one genre, so I need a data type which supports this feature. In access I could link one table 'genre' to the field 'genre' in my table 'movies', so I could choose none, one ore multiple genres per movie. When I switched to MySQL I used the SET data type to define all the possible values. So far everything is running perfectly.
I am now trying to set up a table in html/php to show the mysql table. I want the table to be able to sort on: title, genre, quality, rating, etc. But for the sorting on genre, I would need the possible values from the set data type. I don't know if it is possible to get the values with some php command/code, but after I lurked around on the web for a while, I didn't see many applications where they use the SET data type for obvious negative reasons.
So I started looking into the Foreign Key possibility. The problem I have here is that -for as far as I know- the key can only contain one possible value, which puts me right back at the start of my problem. I do like the idea of a foreign key, because it would make it way easier for me to add a new genre to the list.
Is there a possibility I am overlooking? Is it possible to either get the values from the SET type to php or to use a foreign key with multiple possibilities for one record?
I know I can also put every genre in my php script manually, but I'd like to have it all on one place. So that if I add a movie with a genre I haven't defined yet, I can just update it at one place and everything else adapts to it.
Dagon is absolutely right here - you have an issue with the structure of the tables in your back end. You are wanting to model a many to many relationship when at the moment with your current back end the best you can do is a one to many relationship.
To review:
You have individual films that can have many genres
And you have individual genres that are related to many films
Relational databases actually don't model many to many relationships with one relationship they use recursion of the one to many relationship and create two joins.
To model a many to many relationship you need three tables
A film table (which I think you already have)
A genre table (which I think you already have)
A junction table which as Dagon suggests will consist of two fields film id and genre id.
You then set up two separate one to many relationships. One from the film table to the junction table and one from the genre table to the junction table.
Now if you want to know all the genres a film is in you simply filter the junction table on the relevant film id and if you want to know all the films with a certain genre you filter the junction table on the genre id.
Set up lookups to relate your genre ids to textual descriptions and bang you are free to change the textual description as much as you want and the great thing if you've done it right it will upgrade every single value in your forms.
This is an absolute fundamental concept of the algebra of sets behind the design of SQL and relational database design.
So I'm a visual designer type guy who has learned a respectable amount of PHP and a little SQL.
I am putting together a personal multimedia portfolio site. I'm using CI and loving it. The problem is I don't know squat about DB design and I keep rewriting (and breaking) my tables. Here is what I need.
I have a table to store the projects:
I want to do fulltext searcheson titles and descriptions so I think this needs to be MyISAM
PROJECTS
id
name (admin-only human readable)
title (headline for visitors to read)
description
date (the date the project was finished)
posted (timestamp when the project was posted)
Then I need tags:
I think I've figured this out. from researching.
TAGS
tag_id
tag_name
PROJECT_TAGS
project_id (foreign key PROJECTS TABLE)
tag_id (foreign key TAGS TABLE)
Here is the problem I have FOUR media types; Photo Albums, Flash Apps, Print Pieces, and Website Designs. no project can be of two types because (with one exception) they all require different logic to be displayed in the view. I am not sure whether to put the media type in the project table and join directly to the types table or use an intermediate table to define the relationships like the tags. I also thinking about parent-types/sub-types i.e.; Blogs, Projects - Flash, Projects - Web. I would really appreciate some direction.
Also maybe some help on how to efficiently query for the projects with the given solution.
The first think to address is your database engine, MyISAM. The database engine is how MySQL stores the data. For more information regarding MyISAM you can view: http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html. If you want to have referential integrity (which is recommended), you want your database engine to be InnoDB (http://dev.mysql.com/doc/refman/5.0/en/innodb-storage-engine.html). InnoDB allows you to create foreign keys and enforce that foreign key relationship (I found out the hard way the MyISAM does not). MyISAM is the default engine for MySQL databases. If you are using phpMyAdmin (which is a highly recommended tool for MySQL and PHP development), you can easily change the engine type of the database (See: http://www.electrictoolbox.com/mysql-change-table-storage-engine/).
With that said, searches or queries can be done in both MyISAM and InnoDB database engines. You can also index the columns to make search queries (SELECT statements) faster, but the trade off will be that INSERT statements will take longer. If you database is not huge (i.e. millions of records), you shouldn't see a noticeable difference though.
In terms of your design, there are several things to address. The first thing to understand is an entity relationship diagram or an ERD. This is a diagram of your tables and their corresponding relationships.
There are several types of relationships that can exist: a one-to-one relationship, a one-to-many relationship, a many-to-many relationship, and a hierarchical or recursive relationship . A many-to-many relationship is the most complicated and cannot be produced directly within the database and must be resolved with an intermittent table (I will explain further with an example).
A one-to-one relationship is straightforward. An example of this is if you have an employee table with a list of all employees and a salary table with a list of all salaries. One employee can only have one salary and one salary can only belong to one employee.
With that being said, another element to add to the mix is cardinality. Cardinality refers to whether or not the relationship may exist or must exist. In the previous example of an employee, there has to be a relationship between the salary and the employee (or else the employee may not be paid). This the relationship is read as, an employee must have one and only one salary and a salary may or may not have one and only one employee (as a salary can exist without belonging to an employee).
The phrases "one and only one" refers to it being a one-to-one relationship. The phrases "must" and "may or may not" referring to a relationship requiring to exist or not being required. This translates into the design as my foreign key of salary id in the employee table cannot be null and in the salary table there is no foreign key referencing the employee.
EMPLOYEE
id PRIMARY KEY
name VARCHAR(100)
salary_id NOT NULL UNIQUE
SALARY
id PRIMARY KEY
amount INTEGER NOT NULL
The one-to-many relationship is defined as the potential of having more than one. For example, relating to your portfolio, a client may have one or more projects. Thus the foreign key field in the projects table client_id cannot be unique as it may be repeated.
The many-to-many relationship is defined where more than one can both ways. For example, as you have correctly shown, projects may have one or more tags and tags may assigned to one or more projects. Thus, you need the PROJECT_TAGS table to resolve that many-to-many.
In regards to addressing your question directly, you will want to create a separate media type table and if any potential exists whatsoever where a project is can be associated to multiple types, you would want to have an intermittent table and could add a field to the project_media_type table called primary_type which would allow you to distinguish the project type as primarily that media type although it could fall under other categories if you were to filter by category.
This brings me to recursive relationships. Because you have the potential to have a recursive relationship or media_types you will want to add a field called parent_id. You would add a foreign key index to parent_id referencing the id of the media_type table. It must allow nulls as all of your top level parent media_types will have a null value for parent_id. Thus to select all parent media_types you could use:
SELECT * FROM media_type WHERE parent_id IS NULL
Then, to get the children you loop through each of the parents and could use the following query:
SELECT * FROM media_type WHERE parent_id = {$media_type_row->id}
This would need to be in a recursive function so you loop until there are no more children. An example of this using PHP related to hierarchical categories can be viewed at recursive function category database.
I hope this helps and know it's a lot but essentially, I tried to highlight a whole semester of database design and modeling. If you need any more information, I can attach an example ERD as well.
Another posibble idea is to add columns to projects table that would satisfy all media types needs and then while editting data you will use only certain columns needed for given media type.
That would be more database efficient (less joins).
If your media types are not very different in columns you need I would choose that aproach.
If they differ a lot, I would choose #cosmicsafari recommendation.
Why don't you take whats common to all and put that in a table & have the specific stuff in tables themelves, that way you can search through all the titles & descriptions in one.
Basic Table
- ID int
- Name varchar()
- Title varchar()
etc
Blogs
-ID int (just an auto_increment key)
-basicID int (this matches the id of the item in the basic table)
etc
Have one for each media type. That way you can do a search on all the descriptions & titles at the one time and load the appropriate data when the person clicked through the link from a search page. (I assume thats the sort of functionality you mean when you say you want to be able to let people search.)
product table has category, media table has category, ticket table has category.
Each of these has a HasMany relation with category table. There are two ways of doing it:
Have a common Category table with probably a type column and have intermediatory table like MediaCategory, etc.
Have separate tables like MediaCategory with each having same structure as category
First one is better I think in point of integrity.
IF the categories are not shared then it is best (in most cases) to have separate tables for each category type.
Here's the rationale:
The database is the gatekeeper of your data's relational integrity. In a well written program there should not be any foreign key violation exceptions, i.e. the code does not rely on the database to keep the relational integrity of the data. However should a bug creep-in, the relations in the database make the bug less likely to cause data corruption.
When using separate tables for media, products, etc. and their valid categories, the relational integrity can be easily maintained with a foreign key relationship; essentially any record in the media table can belong to any category in the media categories table. Ensuring the relation:
"Records in media table can belong to any category of type 'media' in
the categories table"
is less straight forward at the database level.
That being said, a problem whose solution is duplication of data structures makes the whole underlying structure suspect. This may not be so in your case, but you should look at the underlying use-cases that require the categories to be introduced and see whether they are better served in a different manner (say Free Text Search indexed keywords.)
I would like to have categories, and rankings for my content and users respectively, and I am not sure how to go about implementing this using CakePHP conventions?
Do I need to create a model?
That depends entirely on what these categories are supposed to do and not do. You could simply define a number of constants that you use for categorizing stuff. But, are categories...
subject to change? Do you want to add more eventually?
editable? May you want to change their names?
nested?
supposed to have more attributes than just their id? Names, descriptions?
If you answered Yes to any of the above, you'll want to store them as data in the database. This is independent of Cake, it's just sane data modeling. For Cake that means you'll need to create a model. The same goes for ratings.
So you'll have these tables:
users
hasMany ratings
categories
hasMany contents
contents
belongsTo categories
hasMany ratings
ratings
belongsTo users (polymorphic)
belongsTo contents (polymorphic)
You may want to separate user ratings and content ratings into two tables instead of using a combined polymorphic table (which just means that you have an extra column keeping track of whether a rating is for a user or for content).
i guess you are looking for something like this IF you dont want to use a model:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
one possible approach to use "enums" for things that maybe only have 1-5 states.
if you have more than 10 or you want to be able to dynamically modify them (label, active/inactive) you will need a separate table and model relation.