1

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

4
  • And what if you force array creation when you insert the comment: $comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment", "replies" => []]); ? Commented May 31, 2015 at 9:50
  • @SylvainLeroux that save ok but in this form I can't use #jenssegers-mongodb library features so delete method still doesn't work Commented May 31, 2015 at 9:55
  • Could you explain "delete method still doesn't work" : is there some error message ? Or the reply is simply not deleted ? Maybe the data are still not saved with the the correct format ? I don't know Laravel, so I can't help you much, but maybe worth showing us how you tried to delete the reply ? At this point, it is hard to tell (at least to me) if you have an issue when you insert your data or when you delete them... Commented May 31, 2015 at 12:10
  • @SylvainLeroux when I use $comment->delete() comment will be deleted but when I use $reply->delete() delete does not work! Commented May 31, 2015 at 13:06

1 Answer 1

2

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"

            }

        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.