我有一个带有embedsMany模型评论的模型新闻,在模型评论中我有embedsMany模型回复
当我这么做时:
$new = News::create(["title"=>"Simple new", "body"=>"this is a simple news"]);
$comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment"]);插入是OK的,DB中的数据是:
{
"title" : "Simple new",
"body" : "this is a simple news",
"_id" : ObjectId("5569b157bed33066220041ac"),
"comments" : [
{
"subject" : "comment 1",
"body" : "my comment",
"_id" : ObjectId("5569cc28bed330693eb7acd9")
}
]
}但当我这么做的时候
$reply = $comment->replies()->create(["subject"=>"reply to comment 1", "body"=>"my reply"]);DB是:
{
"title" : "Simple new",
"body" : "this is a simple news",
"_id" : ObjectId("5569b157bed33066220041ac"),
"comments" : [
{
"subject" : "comment 1",
"body" : "my comment",
"_id" : ObjectId("5569cc28bed330693eb7acd9"),
"replies" : {
"0" : {
"subject" : "reply to comment 1",
"body" : "my reply",
"_id" : ObjectId("5569cc33bed330693eb7acda"
}
}
}
]
}删除回复不起作用
发布于 2015-06-17 12:06:58
解决方案1:
在jenssegers/laravel框架中,您可以使用push或update方法将文档插入到数组中。注意:在早期版本中不存在push方法。
解决方案2: (推荐)
Mongodb使用模式库框架(即nosql,无模式数据库)是错误的,建议在php中使用官方Mongodb框架:
http://docs.mongodb.org/ecosystem/drivers/php
为了加快查询速度,可以使用索引。
在此解决方案中,您可以使用以下数据结构:
{
"_id" : ObjectId("5577d8a419e5eae7ae17a058"),
"name" : "My New",
"comments" : {
"5577d8c419e5eae7ae17a059": {
"subject" : "My Comment",
"body" : "BODY",
"replies" : {
"5577d91619e5eae7ae17a05b": {
"subject" : "My Reply",
"body" : "BODY"
}
}
}
}https://stackoverflow.com/questions/30556002
复制相似问题