Reputation: 64
I have a model News with embedsMany model Comment and in model Comment I have embedsMany model Reply
when I do this:
$new = News::create(["title"=>"Simple new", "body"=>"this is a simple news"]);
$comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment"]);
Insert is OK and data in DB is:
{
"title" : "Simple new",
"body" : "this is a simple news",
"_id" : ObjectId("5569b157bed33066220041ac"),
"comments" : [
{
"subject" : "comment 1",
"body" : "my comment",
"_id" : ObjectId("5569cc28bed330693eb7acd9")
}
]
}
but when I do this:
$reply = $comment->replies()->create(["subject"=>"reply to comment 1", "body"=>"my reply"]);
DB is:
{
"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"
}
}
}
]
}
and delete reply doesn't work
Upvotes: 1
Views: 5468
Reputation: 446
Solution 1:
In jenssegers/laravel-mongodb framework you can use of push or update method to insert documents to an array. Note: push method does not exist in earlier versions.
Solution 2: (recommended)
Use of schema base framework for Mongodb (that is a nosql, schema less database) is wrong and use of official Mongodb framework for php is recommended:
http://docs.mongodb.org/ecosystem/drivers/php
For speed up queries you can use indexing.
In this solution you can use this data structure:
{
"_id" : ObjectId("5577d8a419e5eae7ae17a058"),
"name" : "My New",
"comments" : {
"5577d8c419e5eae7ae17a059": {
"subject" : "My Comment",
"body" : "BODY",
"replies" : {
"5577d91619e5eae7ae17a05b": {
"subject" : "My Reply",
"body" : "BODY"
}
}
}
}
Upvotes: 2