【发布时间】:2015-08-13 21:06:58
【问题描述】:
我有一个带有 embedsMany 模型评论的模型新闻,并且在模型评论中我有 embedsMany 模型回复
当我这样做时:
$new = News::create(["title"=>"Simple new", "body"=>"this is a simple news"]);
$comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment"]);
插入没问题,数据库中的数据是:
{
"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"]);
数据库是:
{
"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"
}
}
}
]
}
删除回复不起作用
【问题讨论】:
-
如果在插入评论时强制创建数组会怎样:
$comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment", "replies" => []]);? -
@SylvainLeroux 可以保存,但在这种形式下我不能使用 #jenssegers-mongodb 库功能,所以删除方法仍然不起作用
-
您能解释一下“删除方法仍然不起作用”:有错误信息吗?还是根本没有删除回复?也许数据仍然没有以正确的格式保存?我不知道 Laravel,所以我帮不了你,但也许值得向我们展示如何你试图删除回复?在这一点上,很难判断(至少对我而言)您在插入数据或删除数据时是否有问题...
-
@SylvainLeroux 当我使用
$comment->delete()时评论将被删除,但当我使用$reply->delete()时删除不起作用!
标签: php mongodb laravel jenssegers-mongodb