Note that there are some explanatory texts on larger screens.

plurals
  1. PORestore document with embedded documents on ODM
    primarykey
    data
    text
    <p>Here are two sample classes I have:</p> <pre><code>/** @ODM\Document */ class Product implements JsonSerializable{ /** @ODM\String */ protected $some_property; /** @ODM\EmbedMany */ protected $attributes; public function jsonSerialize(){ $o = new StdClass(); $o-&gt;property = $this-&gt;some_property; $o-&gt;attributes = $this-&gt;attributes; return $o; } } /** @ODM\EmbeddedDocument */ class Attribute implements JsonSerializable{ /** @ODM\String */ protected $some_property; public function jsonSerialize(){ $o = new StdClass(); $o-&gt;property = $this-&gt;some_property; return $o; } } </code></pre> <p>In my code, I create an instance of Product, and then some processes create an array of Attribute instances on <code>$product-&gt;attributes</code>. I persist the Product instance without a problem into mongoDB using Doctrine ODM. I can go into the DB (using rockmongo), and I see the presisted document, and the annotations on the JSON view to the class of the <code>attributes</code> array:</p> <pre><code>"_doctrine_class_name": "\Attribute" </code></pre> <p>But when I query for that product using the QueryBuilder, instead of getting an array of Attribute instances, I get a PersistentCollection (looking at the isntance with the debugger at runtime).</p> <p>I believe this has to do with <em>lazy loading</em>, but its breaking up my code. When I try to call <code>json_encode($product)</code>, instead of cascading through to each of the <code>Attribtue</code> instances, it just returns an empty array.</p> <p>Here is what I expect to get form a json_encode():</p> <pre><code>{ "property": "some product value", "attributes": [ { "property": "some attribute value" }, { "property": "some attribute value" } ] } </code></pre> <p>Is there any way to either disable <em>lazy loading</em>, or force the proper instantiation of each Attribute instance? Or any other way to be able to get the desired JSON object, without having to manually traversing the whole structure? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload