2

I am using the Lithium PHP Framework - with MongoDB database - to create some input/edit forms for my application.

I'm using the concept of embedded documents in MongoDB. It took me a bit of tuning to get the initial save to work, but I have it set up so it is working properly.

When I look in MongoDB - my object looks as follows:

 {
     _id: "5119951516c19",
     level_name: "Testing",
     questions: [
       {
           question_id: "5119951516cb5",
           question_text: "Why is the sky blue"
       },
       {
           question_id: "5119951516cc3",
           question_text: "Why is grass green"
       }
    ]
 }

Now, if I want to do a very simple edit, such as:

$level = Levels::find($id);
$level["level_name"] = "Testing - UPDATE";
$level->save();

The good news is that the field in question saves. But, the bad news is that it destroys my entire object model. The new object model in MongoDB gets saved as follows:

{
  0: {
    question_id: "5119951516cb5",
    question_text: "Why is the sky blue"
   },
  1: {
    question_id: "5119951516cc3",
    question_text: "Why is grass green"
  },
  _id: "5119951516c19",
  level_name: "Testing - UPDATE",
  questions: [
    {
      question_id: "5119951516cb5",
      question_text: "Why is the sky blue"
    },
    {
      question_id: "5119951516cc3",
      question_text: "Why is grass green"
    }
  ]
}

The basic doc remained the same - but somehow the embedded docs got thrown to the top level under the keys "0" and "1".

I've searched high and low and I can't figure out why this is happening. Since my save function is so simple, I am assuming this is a bug in Lithium. Which is fine - as long as there is a way I can get around it.

Thanks in advance.

NinoCodes
  • 35
  • 1
  • 4
  • 1
    Do you know what version of lithium you are using? What about your version of the php mongo extension? Do you have a schema defined in your Levels model? If so, can you paste it in? I use embedded objects like this all the time, so yes, there should be a way around it. – rmarscher Feb 12 '13 at 02:45
  • Look at updating instead of saving. http://docs.mongodb.org/manual/applications/update/#crud-update-update You can update parts of the document and not re-write the entire thing. – ryan1234 Feb 12 '13 at 02:49
  • @rmarscher - I am new to Lithium so I have all the latest versions. – NinoCodes Feb 12 '13 at 02:54
  • @NinoCodes ok. Do you know if you cloned the master branch from Github? Or downloaded the 0.11 release? I just want to make sure I test it out with the same version. – rmarscher Feb 12 '13 at 03:06
  • 2
    Also, to ryan1234's point, I'm pretty sure the Lithium framework does an update behind the scenes when you call save() - it's not an exact 1-to-1 translation to the mongo shell. – rmarscher Feb 12 '13 at 03:08
  • @rmarscher It looks like I am using Lithium 0.10 right now. Should I upgrade to 0.11? My PHP Mongo Extension is v1.3.2. Also - I am hosting my MongoDB database in the cloud at MongoHQ. Upon further examination, it appears that the error is not on the "Save" or "Update feature so much, but when I use the "Find" command and try to map my data to the model. (To answer your previous question - I don't have a schema at the model. But I do have Validation). – NinoCodes Feb 12 '13 at 14:17
  • @rmarscher - I upgraded to 0.11 and I may be getting somewhere. More to follow shortly.... – NinoCodes Feb 12 '13 at 14:29
  • I believe $level should be an Object. So did you tried to save the Testing data like so: $level->level_name = "Testing - UPDATE"; – Marc Mar 06 '13 at 23:44

0 Answers0