2

I am aware that ODM purpose is mapping, but I'm also curious if I can save a JSON Object without mapping it to any class. Or with mapping it to a class but having only one object value $object.

I've managed to do this when ever I would have an array of objects, for instance:

[  
   {  
      "id":28,
      "Title":"Sweden"
   },
   {  
      "id":56,
      "Title":"USA"
   },
   {  
      "id":89,
      "Title":"England"
   }
]

I've managed to save that array of objects without mapping the id, Title, and other fields which are not there.

/**
 * @MongoDB\Field(name="object", type="hash")
 */
protected $object = array();

My question is if I can do the same thing only for the JSON Objects, not array objects. For instance, I'd like to save this object without mapping every single key:

   {  
      "id":28,
      "Title":"Sweden"
   },
   {  
      "id":56,
      "Location":"New York"
   },
   {  
      "id":89,
      "Something": {
           "test": "test
        }
   }
Dino
  • 7,779
  • 12
  • 46
  • 85

1 Answers1

4

ODM provides you with RawType which just saves whatever is given:

/**
 * @MongoDB\Field(name="object", type="raw")
 */
protected $object;

I'm not sure though if there's a mistake in your desired stored code as there are 3 objects and that you won't have as you can't map 3 objects to 1 property.

malarzm
  • 2,831
  • 2
  • 15
  • 25