I have a table which stores posts created by user on a particular topic.each of these posts belong to one and only one topic. there is a column in the posts table which stores the id of the topic. I would like to display all title of the posts in a page categorized under topic name
example:
Animals
Post 1
Post 2
Post 3
Plants&Trees
Post 1
Post 2
Post 3
My 'posts' table is in following format
id
posttitle
posttext
topic_id
My 'topics' table is in following format
id
topicname
What relationship have to define so that i can access the topicname and id in the following name.
foreach($posts as $post)
echo $post->topics->topicname
To say it in words, each topic has many posts and each post belongs to a topic - that is exactly what relationships you need.
When you have defined them, as described in the documentation, you can do something like this:
$topics = ORM::factory("topic")->find_all();
foreach ($topics as $topic) {
// $topic->topicname
foreach ($topic->posts as $post) {
// $post->posttitle
}
}
Related
I have a REST API which serves from a database in MongoDB. Say each JSON unit that is served contains data about a film. I'd like my users to tick whether they've seen a film or not. My problem is that, being a beginner, I am not sure how this should be implemented.
I somehow need to save this for each user. Right now my mongo database just holds all of the films in general.
Could you give me an example of how this would be accomplished in a no-sql database? What if I want to save more detailed data like when a user takes private notes related to a certain film?
You can simply have a table that has the user_id, the movie_id and the boolean, which mean if there is movie_id and a user_id the boolean is true if not it remains false thats how i would impplement it.
You can have movies data some what like this which contains unique ID for each movie
movie={
id : <MOVIE_ID> //some movie ID
name : <MOVIE_NAME> //some name
... //other info you want to store
}
then you can save each movie that user had watched in his user data in a array
user = {
id : <USER_ID> //some user id
name : <USER_NAME> //some name
watched_movies : [<MOVIE_ID1>,<MOVIE_ID2>,.....] //movie IDS
... //other data about user
}
while you rendering all movies for particular user you just need to traverse these array and mark check for movie if user have already watched that movie
I am trying to pull a Post object from my MongoDB. This Post has an author field, which is an ObjectID referencing who authored the blog post. Once I fetch the blog post, I want to fetch the Author's username and replace the ID in the post object before sending it out to as a response to clients.
When running the code, the correct Post shows when I log it, and the correct Username is shown when it is fetched, but I can never modify the object itself.
BlogController.getPost(id, (error, result) => {
if (error) res.send({success:false, message:error})
else {
var post = result
AuthController.getUserByID(post.author, (error, resultNested) => {
if (error) res.send({success:false, message:error})
else {
post.author = resultNested.username
console.log(post)
console.log(resultNested)
res.send({success:true, message:post})
}
})
}
})
I would include the console log outputs to show you the structure of the objects and that the values I am trying to modify xist, but the new StackOverflow UI makes inputting code even more of a pain in the ass than the past. Somehow...
I expect for the post object to have it's authorfield modified, but the author field remains an ObjectID.
I would suggest storing the name of the author in the post model as a start. MongoDB is referred to as a 'Document Based' DB and I therefore like to think of each record in a collection as a standalone 'document'.
I can understand the logic of the relational model you are trying to implement, but since MongoDB stores data in JSON format - one could argue that there is some room for redundancy.
I would recommend...
post{
author : string;
.
.
.
relUser : string;
}
Where the relUser field will be the id of the user that posted the blog entry and we store the author/username in the author field. In this way you simplify the number of calls you need to make, by getting more info per single call.
-------------------
Schema Notification
-------------------
id,
userId,
status,
...
-----------
Schema User
-----------
id
name
email
...
I have a model named Notification in loopback, which roughly has
id, userId, ..... and some other fields. Here userId is a relation to user table which has name and other details of user.
What I have:
At the front end, I have displayed all these notifications in a paginated form (10 records per page).
Front-end View:
+------------------------------------------+
| UserName UserEmail NotificationStatus |
+------------------------------------------+
| xxxx xxx#xx.xxx PENDING |
| xxxx xxx#xx.xxx ACCEPTED |
| xxxx xxx#xx.xxx REJECTED |
--------------------------------------------
What I want:
To be able search through the notifications based on name in the user relation.
What I already know:
is that loopback already provides something like:
include: [{
relation:"user" ,
scope:{
where:{
name: { like: 'name-of-user', options: 'i'}
}
}
}]
What It returns:
is all the notifications but only include users that has name matching with the string I passed. i.e. prints all the notifications, but leaves the user names and email that are not matching with the string I passed.
What I thought to try:
is to remove the entries in my angular front-end controller, which do not have user Object just before assigning the http response to a scope variable which loops through entries in html. But let's say if 5 out of 10 entries will not have users, angular will splice out those entries but actually there must be 10 entries per page unless there are no records matching the criteria.
I hope I made sense in question, and seek for some solution on the matter.
Thanks
This is not possible as of now with loopback if you are using rdbms type of schema .
Look at some open issues .
https://github.com/strongloop/loopback/issues/517 https://github.com/strongloop/loopback/issues/683
you might need to look for native sql for this issues
Im working on some sort of social network in which people are able to make posts about a topic and like them.
Im having trouble tracking user likes.
The schema is the following:
Users:
{ userId: "someId", likes: ["idPost1", "idPost4", ...] }
Posts:
{ postId: "someId", topic: "idTopic", postContent: "someContent"}
I need a query that can:
Take all posts from a certain topic, like this:
r.table('posts').filter({
topic: idTopic
}).run().then( posts => res.json(posts))
Look up to see if the current user (given by the user id) has liked any of the posts on that specific topic. Then return a JSON with all posts on that topic, and those liked by the user with "liked: true".
Im having trouble with step 2,
Please let me know if im modelling data the wrong way, or if you can think of any way I can accomplish step 2.
Thanks!
This function does what you're looking for in javascript. (Tested using the Data Explorer)
r.db("test").table('posts').filter({ topic: "idTopic" }).merge(function(post){
return {liked: r.table('users').filter({userId: "someId"})(0)('likes').contains(post("postId")) }; })
The merge function basically iterates every post and adds the field liked to it. The contains function checks whether the posts id is in the likes array and return in directly as truth value.
Is it appropriate that my collections have a key call session so that I can identify from whom this data belongs to? For example, I have few sets of data that store books. How to identify in nosql DB(MongoDB) that a set of data belongs to which user? I know in mysql we simply design the table using Foreign Key, but how can I do it in nosql?
What I can think of is I will have these data :
{
bookId:1,
bookName: "soemthing",
userId:1
}
{
another_collection_key:1,
another_value: "soemthing",
userId:1
}
where every set of data will have userId, correct?
The best way is to create a user collection and a book collection. In each book collection add a list of type 'user'. Sample collections given below -
User{id,name}
Book{id,name,list<user>}
This way each book can store all the users who have that book.
The other way is to create 3 collections - books, users and a link collection for linking book and user.
Sample collections given below -
User { id,name }
Books { id,name }
Lnk_User_Book { User_Id,Book_Id }.