I am doing a simple blog project for which I've come up with the following object class structure.
user class (email address, password)
blog_entry class ( title, body, dateposted, state[draft, published] )
comment class ( comment, date posted )
category class ( category name )
object relationships
a user can post zero or more blog entries, a blog entry belong to one and only one user
a user can publish one or more comments, a comment belong to one and only one user
a comment belong to a one and only one blog entry, a blog entry can have zero or more comments
a blog entry belong to one and only one category, category can exist without a blog entry
I need to know how to map these object structure(including relationships) into a relational data model for mysql. Since this is my first MVC project I don't have a clear understanding how to get this done properly.
Pl. advise.
PS: I've come up with following data model. Appreciate your ideas on this:
users
- id
- email_address
- password
categories
- id
- category_name
blog_posts
- id
- post_title
- post_contents
- post_dateposted
- post_state
- users_id
- categories_id
comments
- id
- comment
- comment_dateposted
- blog_posts_id
- users_id
Thanks.
Related
I'm looking for a way to best analyse the user data in our app.
for example
how many users have read 0 articles on our site
how many users have read 1 article on our site etc
we have a users table with id, username columns and we also have an activities table that creates an entry when an article is viewed. For example it would create a database row with
id
activity
user_id
1
read
1
all of the data we need is there, I just don't know how to interrogate to give that detail.
I personally would use an int code for activity and not a string. So "read" would = 1, "edit"=2...etc If you have no care about what user read with no association, that becomes easy by just setting a flag on the article that was read by article id. So your article table would have:
id article_id activity user_id
1 5 1 8675309
From there just do an eloquent query on article_id where activity = 1...
$articlecount = Article::where('article_id', $request->id)->where('activity', 1)->count();
$articlecount will give you all of the reads for that article.
If you need it based on user you could do a one to many relationship from your users models to articles models. Then query the user with(articles). That will bring back all the articles where that user has activity. You could also specify in your eloquent to only bring back activity of reads/edits..etc too. In the same respect you could do the reverse and query the article and see all the users that have read that same article.
I'm building a small forum app where a user has a profile and on that profile, the user who owns the profile as well as other users, can make posts ( pretty much the same concept as with fb ).
My concern is whether it is better to have multi-word classes or whether it is better to have a separate class for each word.
For example, if i have a profile on which i can create posts.
Would it be better to have a model Profile and a model Posts
Or it is better to have a ProfilePost model
My approaches so far are the following
The tables for the first approach
users
- id
profile
- id
- user_id
posts
- id
- body
- profile_id
comments
- id
- body
- poster_id
- post_id
Where poster is the user who creates the post
The user has a profile
The profile has posts
Posts have comments
As a result, if i want to add a new post, then i'd have to make multiple joins, for example
$user->profile()->posts()->addBy($anotherUser, $post)
$user->profile()->post($post)->addCommentBy($comment, $anotherUser)
The tables for the second approach are
users
- id
profile_posts
- id
- body
- profile_user_id
- poster_id
comments
- id
- poster_id
- profile_post_id
And the approach to create a post and a comment will be like
$user->postToProfile($profileUser, $post);
$user->profilePost($post)->addComment($comment);
The first approach requires multiple joins but the naming seems more elegant to me.
While the second approach consists of multiwords such as profilePost which seems like these words can be extracted into their own classes.
I am completing some work for Uni and have a single table of data that i need to arrange into 1st,2nd and 3rd Normal Form, i have attempted to do so below but i have hit a brick wall with it.
I would appreciate any helpful input at all as my tutor is away on holiday and I don't want to start the rest of the work using the wrong tables/relations to start with.
Basically the system is supposed to allow users to add films, directors and actors. Allow multiple users to review films and categorise the films by genre.
UNF
**filmID**
title
directorID
categoryID
categoryName
notes
directorName
actorName
actorID
role
userID
userName
reviewDate
reviewText
1NF
**filmID**
title
notes
**directorID**
directorName
**categoryID**
categoryName
**actorID
filmID***
actorName
role
**userID**
userName
**filmID*
userID***
reviewDate
reviewText
2NF
**filmID**
title
notes
**directorID**
directorName
**categoryID**
categoryName
**actorID**
actorName
**actorID
filmID***
role
**userID**
userName
**filmID*
userID***
reviewDate
reviewText
You'll find here on Stack Overflow that people won't answer your homework for you.
However here's a link to a very good tutorial.
Normalisation Tutorial
You'll understand it much better if you work through it and complete it yourself, rather than copying and pasting the answer from here.
Here are some questions for you...
Can an actor also be a director?
Can a film have more than one director?
Can a user review a film more than once?
Are the genres predefined?
Can an actor play more than one role in a film?
It's entirely possible that a database that meets the criteria for 2nd Normal Form also meets the criteria for 3rd Normal Form. It looks like that is the case here.
I have three tables:
Stories (id, category_id, sub_category_id, name, story),
Categories (id, parent_id, lft. rght, name),
SubCategories (id, name)
They are properly related and all is working fine. But now I need to find stories which belongs to specified category and/or sub_category, by name autocompleate dialog. Example: user entered "dog bone" and must search for such a name in categories/subcategories and after find all stories whitch belongs to found categories. No problem when doing many finds, but in SQL I can make one query. Can this be done in CakePHP in one find ?
Thank you very much!
there are a few options for doing this, some of them below
a) linkable behavior - https://github.com/Terr/linkable/wiki
b) bindModel - http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
c) adhoc-joins http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find
read up on them and see what suites your needs best.
either you solve it with the table assocations or you use "containable" behavior.
"Containable" Behavior is very easy to use and easy to implement.
Containable Behavior in Cookbook
I'm currently logging all actions of users and want to display their actions for the people following them to see - kind of like Facebook does it for friends.
I'm logging all these actions in a table with the following structure:
id - PK
userid - id of the user whose action gets logged
actiondate - when the action happened
actiontypeid - id of the type of action (actiontypes stored in a different table - i.e. following other users, writing on people's profiles, creating new content, commenting on existing content, etc.)
objectid - id of the object they just created (i.e. comment id)
onobjectid - id of the object they did the action to (i.e. id of the content that they commented on)
Now the problem is there are several types of actions that get logged (actiontypeid).
What would be the best way of retrieving the data to display to the user?
The easiest way out would be gabbing the people the user follows dataset and then just go from there and grab all other info from the other tables (i.e. the names of the users the people you're following just started following, names of the user profiles they wrote on, etc.). This however would create a a huge amount of small queries and trips to the database in a while loop. Not a good idea.
I could use joins to retrieve everything in one massive data set, but how would I know where to grab the data from in just one query? - there's different types of actions that require me to look into several different tables to retrieve data, based on the actiontypeid...
i.e. To get User X is now following User Y I'd have to get my data (User Y's username) from the followers table, whereas User X commented on content Y would need me to look in the content table to get the content's title and URL.
Any tips are welcome, thanks!
Consider creating several views for different actiontypeids. Union them to have one full history.