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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • And what if you _force_ array creation when you insert the comment: `$comment = $new->comments()->create(["subject"=>"comment 1", "body"=>"my comment", "replies" => []]);` ? – Sylvain Leroux May 31 '15 at 09:50
  • @SylvainLeroux that save ok but in this form I can't use #jenssegers-mongodb library features so delete method still doesn't work – Ehsan Farajzadeh May 31 '15 at 09: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... – Sylvain Leroux May 31 '15 at 12:10
  • @SylvainLeroux when I use `$comment->delete()` comment will be deleted but when I use `$reply->delete()` delete does not work! – Ehsan Farajzadeh May 31 '15 at 13:06

1 Answers1

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"

            }

        }

    }

}
Reza Roshan
  • 147
  • 3
  • 7