I am building an app using back4app which is base on parse-platform database.
my database schema is like this:
Users table:
id
User_name
....
Items table:
id (f)
Item_name
Count
.....
User item table:
Id
User_id
Item_id
item_status
item_notes
......
what I want to achieve is to get the item_name by having the user_id how this can be possible using parse in android?
Edit: to be clear ... every item will be defined in the User_Item table even if it's the same item because the user may change at any time.
that's why I want the user_id to get the item_name from the items table.
I'm assuming you want to query the 'User item table' so...
First of all your itemId field should be a pointer to the object in the 'Items table', this way you can use query.include("itemId"); which means that the object from the 'Items table' will also be returned with the query.
Below is an example of a full query for this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserItem");
query.include("itemId");
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
ParseObject item = object.getParseObject("itemId");
String itemName = item.getString("Item_name");
}
});
I don't use the android SDK so this code may not be perfect - please see the android guide for more details (I lifted all this code from there).
Edit:
Add query.whereEqualTo("User_id", "xxxxx"); to get the User item for the current user (replace xxxxx with the users Id).
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserItem");
query.include("itemId");
query.whereEqualTo("User_id", "xxxxx");
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
ParseObject item = object.getParseObject("itemId");
String itemName = item.getString("Item_name");
}
});
Related
Hi there I'm new to parse, android and stackoverflow.com, here is my question
I've two classes on parse one is "post" and other is "user" class. Anyone can like post and dislike post.
I've created two relation column "whoLiked" and "whoDisliked" which points the list of users who has liked/disliked a specific post.
When I'm showing the post to the user I want the current User to like/dislike the post Only once which I've set properly.
But only thing I'm not getting is how can I fetch only currentuser from thisPost object.
I don't wanna fetch the list of user who liked this post I just want to know if current User has liked it or not.?
I'm new to stackoverflow.com plz ignore if any mistakes here. Thanks u all in advance.
Below is the query to fetch all the liker
ParseObject post = ...
// create a relation based on the authors key
ParseRelation relation = book.getRelation("whoLiked");
// generate a query based on that relation
ParseQuery query = relation.getQuery();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> comments, ParseException e) {
if (e == null) {
// "user" is now a list of the user who liked
} else {
// Something went wrong...
}
}
});
Just add a constraint to check if the objectId is the same as the currentUsers's objectId:
// create a relation based on the authors key
ParseRelation relation = book.getRelation("whoLiked");
// generate a query based on that relation
ParseQuery query = relation.getQuery();
// ADD CONSTRAINT HERE:
ParseUser currentUser = ParseUser.getCurrentUser();
query.whereEqualTo("objectId", currentUser.getObjectId());
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> comments, ParseException e) {
if (e == null) {
// "user" is now a list of the user who liked
} else {
// Something went wrong...
}
}
});
I have a problem querying the data of a user using pointer, so this is my tables (class):
_User
objectId<string> | username<string> | ...
Posts
objectId<string> | post<string> | writerId <pointer>(_User)
The writerId contains the user id of the writer of the post.
I'm showing the posts in a custom ListView in this way:
variables_posts = new ArrayList<VariablesPosts>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("post"));
query.include("writerId");
on = query.find();
for (ParseObject post : on) {
VariablesPosts map = new VariablesPosts();
map.setWriter((String) post.get("username"));
map.setPost((String) post.get("post"));
variables_posts.add(map);}
The problem is that the writerId is a pointer and not a string if I put
map.setWriter((String) post.get("writerId"));
The app will crash, so I used from another help the line query.include("writerId"); to directly get the username but this line:
map.setwriter((String) post.get("username"));
is crashing the app too, when I delete it the app works fine, so in my case how can I get the username from User class by the pointer writerId?
I don't know exact Java syntax, but when you include writerId and then want to get username of that writerId User, you must first get the User object from Post's writerId .. something like:
(String)((ParseUser)post.get("writerId")).get("username")
Your naming of the pointer column is a little misleading as the column does not contain IDs. You should call it writer instead.
To get the username, do this:
query.include("writerId");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
for (ParseObject post : postList) {
ParseObject writer = post.getParseObject("writerId");
String userName = writer.getString("username");
// .. (do whatever with userName)
}
} else {
Log.d("post", "Error: " + e.getMessage());
}
}
});
How set objectId with Parse.com with primary key.Can you help me ?
When i create new row, i want setObjectId of row.
final ParseObject parseObject = new ParseObject(ChapsModel.PARSE_OBJECT);
parseObject.put(ChapsModel.PARSE_FIELD_NAME_CHAPS,
chapsModel.get(i)
.getNamChap());
parseObject.put(ChapsModel.PARSE_FIELD_LINK_CHAP,
chapsModel.get(i)
.getLinkChap());
parseObject.put(ChapsModel.PARSE_FILED_TEAM_TRANSLATE,
chapsModel.get(i)
.getTeamTranslate());
parseObject.put(ChapsModel.PARSE_FIELD_OBJECT_MANGA,
ParseObject.createWithoutData(MangaModel.PARSE_OBJECT,
chapsModel.get(i)
.getObjectManga()));
parseObject.saveInBackground(new SaveCallback() {
#Override
public void done(final ParseException e) {
if (e == null) {
Log.e(">>>>>",
"done" + ojectId);
// parseObject.setObjectId(ojectId);
} else {
Log.e(">>>>>",
"else" + e.getMessage());
}
}
});
Log
java.lang.RuntimeException: objectIds cannot be changed in offline mode.
Log:
Sorry my english. thank
It's not possible to set objectId with Parse.com with primary key.
Although parse.com doesn't allow us to set the objectId of a row, you can create a new column named anything you want and you can set that new column to any objectId you want. For example, you can create a new column named myObjectId and set it to a string.
From the parse.com website at https://www.parse.com/docs/js/guide#cloud_code
The Data Browser
The Data Browser is the web UI where you can update and create objects in each of your apps. Here, you can see the raw JSON values that are saved that represents each object in your class.
When using the interface, keep in mind the following:
The objectId, createdAt, updatedAt fields cannot be edited (these are set automatically).
These are the columns of my Follow Table:
user (Pointer _User)
follower (Pointer _User)
What I want to do is to get all the user where
follower = currentUser
user.username begins with a certain string
I know that in lower-lever db (like mySQL) these data can all be fetched with a single query.
Is it possible in parse? If not, what's the best way to do such a thing?
Assuming your currentUser object is a PFUser
Note... I am program with parse in objective-c so I am not 100% sure this will run. But I feel like this is what your looking for... Your going to have to first query for all the followers who's follower field is equal to the current user.
Then join that query with a user query whose usernames or names, whatever, are equal to your string you pass in... beware the string you pass into the query is case-sensitive... i think...
So if a user in your app searches for BOB and the database has Bob stored... the query won't return Bob... It will just return nothing....
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("username", certainString);
ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("follower", ParseUser.getCurrentUser());
followerQuery.matchesKeyInQuery("user", "follower", userQuery);
userQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
// comments now contains the comments for posts with images.
}
});
EDIT Going through Parse's Anypic project that does what you want... I feel that this is not possible... Your going to have to first make a query for the users in the followers table whose followers key is the current user. Then find the objects. Once the result is returned, iterate through the objects to whose names is contained within the contains string.
ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("username", certainString);
followerQuery.whereNotEqualTo("username", ParseUser.getCurrentUser().username);
followerQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
for (ParseObject element : list) {
if (element.user == ParseUser.getCurrentUser()) {
//This is a follower of the current user... append to a list object or whatever
}
}
// update a label or table once the iteration is done.
}
});
There still may be a solid solution for what you want to do in one query but I can't think of it right now... Ill come back to this post if I do.
I am trying to query last 20 rows from my Parse.com table. I have followed tutorial and generated the code below. The code is returning 20 rows from the table but not last 20 rows. I returns first 20 items. How to retrieve only last 20 rows?
ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
}
});
It seems you would only need a different sort method on the ParseQuery you are making. You can order the query results by the CreatedDate field:
// Sorts the results in descending order by the Created Date field
query.orderByDescending("dateCreated");
So, in your code, it would be something like:
//Where "CreatedDate", set the name of the Created Date field in your table.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.orderByDescending("CreatedDate").setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
}
});
In the end, you get also the first 20 results, but the results are ordered the other way around, so that should work as intended.
I had the challenge to order ascending but want to get the last record anyhow.
The solution is simple:
query.ascending("your col");
query.find({
success: function(result){
var lastRow = (result.length) - 1;
var highestValue = result[lastRow].get("your col");
}, error: function //... and so on
Sometimes you don't want to order descending ...
Hope I could give something back to this great forum which helps me a lot!!