-
Notifications
You must be signed in to change notification settings - Fork 31
2.06 数据关联
winse edited this page Jul 5, 2015
·
3 revisions
数据可以和其他数据进行关联(使用BmobPointer关联类型),就像是传统数据库中的主外键关系一样,如:一条微博由一个用户发布,可以有多个用户评论,每条评论信息对应一个用户。这时候,微博表对应的对象模型就应该如下:
public class Weibo : BmobTable
{
// 发布的微博
public string message { get; set; }
// 微博的作者
public BmobPointer<BmobUser> user { get; set; }
// 微博的图片地址
public string pic;
public override void readFields(BmobInput input)
{
base.readFields(input);
this.message = input.getString("message");
this.user = input.Get<BmobPointer<BmobUser>>("user");
this.pic = input.getString("pic");
}
public override void write(BmobOutput output, Boolean all)
{
base.write(output, all);
output.Put("message", this.message);
output.Put("user", this.user);
output.Put("pic", this.pic);
}
}
评论表对应的对象模型就应该如下:
public class Comment : BmobTable
{
// 用户的评论
public string comment { get; set; }
// 发布评论的用户
public BmobPointer<BmobUser> user { get; set; }
// 评论的微博
public BmobPointer<Weibo> weibo { get; set; }
public override void readFields(BmobInput input)
{
base.readFields(input);
this.comment = input.getString("comment");
this.user = input.Get<BmobPointer<BmobUser>>("user");
this.weibo = input.Get<BmobPointer<Weibo>>("weibo");
}
public override void write(BmobOutput output, Boolean all)
{
base.write(output, all);
output.Put("comment", this.comment);
output.Put("user", this.user);
output.Put("weibo", this.weibo);
}
}
保存带有关联关系的评论表的数据的方法和保存其他数据模型的方法一样,还是使用BmobUnity对象的Create方法,示例代码如下:
//获取当前登录用户信息
GameUser user = BmobUser.CurrentUser();
var comment = new Comment();
// 设定评论内容
comment.comment = "发布的评论信息";
// 设定评论人
comment.user = new BmobPointer<BmobUser>(user);
// 设定评论对应的微博
Weibo weibo = new Weibo();
weibo.objectId = "ZGwboItm";
comment.weibo = new BmobPointer<Weibo>(weibo);;
bmobUnity.Create(TABLENAME, comment, (resp, exception) =>
{
if (exception != null)
{
print("添加失败, 失败原因为: " + exception.Message);
return;
}
print("添加成功, @" + resp.createAt);
});
关联对象的修改和普通BmobTable对象的修改一样,只需设置要更新的属性值,然后调用Update方法即可。下面假设将objectId为ef8e6agg28的评论记录的作者修改为其他人(这里是直接把当前用户的objectId设置为ZGwboItm)。
GameUser user = BmobUser.CurrentUser();
user.objectId = "ZGwboItm";
Comment comment = new Comment();
// SDK中有添加隐式转换,会把GameUser对象转换成BmobPointer<GameUser>
comment.user = user;
bmobUnity.Update(TABLENAME, "ef8e6agg28", comment, (resp, exception) =>
{
if (exception != null)
{
print("修改失败, 失败原因为: " + exception.Message);
return;
}
print("修改成功, @" + resp.updatedAt);
});
如果你想要查询当前用户发表的所有评论信息,可以跟其他查询一样使用WhereEqualTo,示例代码如下:
BmobQuery query = new BmobQuery();
//按发布时间降序排列
query.OrderByDescending("updatedAt");
//获取当前用户信息
GameUser user = BmobUser.CurrentUser();
//查询当前用户的所有评论
query.WhereEqualTo("user", new BmobPointer<BmobUser>(user));
// or use
// query.WhereMatchesQuery("user", user);
bmobUnity.Find<Comment>(TABLENAME, query, (resp, exception) =>
{
if (exception != null)
{
print("查询失败, 失败原因为: " + exception.Message);
return;
}
//对返回结果进行处理
List<Comment> commentList = resp.results;
foreach (var comment in commentList)
{
print("获取的对象为: " + comment.ToString());
}
});
如果你想要查询带有图片的微博的评论列表,即在Comment表中对Weibo表进行内部的查询,可以使用WhereMatchesQuery方法(DOC:查询的对象中的某个列符合另一个指针值)进行内部查询:
Weibo wb = new Weibo();
// Weibo对象赋值(条件赋值)
BmobQuery query = new BmobQuery();
query.WhereMatchesQuery<Weibo>("pic", new BmobPointer<Weibo>(wb));
bmobUnity.Find<Comment>(TABLENAME, query, (resp, exception) =>
{
if (exception != null)
{
print("查询失败, 失败原因为: " + exception.Message);
return;
}
//对返回结果进行处理
List<Comment> commentList = resp.results;
foreach (var comment in commentList)
{
print("获取的对象为: " + comment.ToString());
}
});
反之,不想匹配某个子查询,你可以使用WhereDoesNotMatchQuery方法。 比如为了查询不带图片的微博的评论列表,就可以将上面的示例代码中的WhereMatchesQuery方法替换为WhereDoesNotMatchQuery方法。
如果你想获取最新的10条评论,同时包含这些评论对应的微博,实现代码可以为如下:
BmobQuery query = new BmobQuery();
// 限制10条
query.Limit(10);
//按创建时间排序
query.Order("createdAt");
//同时将对应的微博信息也查询出来
query.Include("weibo");
//执行查询
bmobUnity.Find<Comment>(TABLENAME, query, (resp, exception) =>
{
if (exception != null)
{
print("查询失败, 失败原因为: " + exception.Message);
return;
}
//对返回结果进行处理
List<Comment> commentList = resp.results;
foreach (var comment in commentList)
{
print("获取的对象为: " + comment.ToString());
}
});
你可以使用.
号(英语句号)操作符来并列获得 Include 中的内嵌的对象。比如,你同时想 Include 一个 Comment 的 weibo 和weibo的 user(微博发布者)对象,你可以这样做:
query.Include("weibo.user");