Note that there are some explanatory texts on larger screens.

plurals
  1. POFOSRestBundle and JMS Serializer, error when geting JSON
    primarykey
    data
    text
    <p>I have been trying out Symfony 2.2, the FOSRest Bundle (using JMS Serializer), and Doctrine ODM using MongoDB.</p> <p>After many hours of trying to figure out how to correctly setup the FOSRest Bundle I'm still having some trouble: I have a very simple route that returns a list of products and prices. <strong>Whenever I request for the HTML format I get the correct response, but if I request any other format (JSON, XML) I get an error:</strong></p> <pre><code>[{"message": "Resources are not supported in serialized data. Path: Monolog\\Handler\\StreamHandler -&gt; Symfony\\Bridge\\Monolog\\Logger -&gt; Doctrine\\Bundle\\MongoDBBundle\\Logger\\Logger -&gt; Doctrine\\Bundle\\MongoDBBundle\\Logger\\AggregateLogger -&gt; Doctrine\\ODM\\MongoDB\\Configuration -&gt; Doctrine\\MongoDB\\Connection -&gt; Doctrine\\ODM\\MongoDB\\LoggableCursor", "class": "JMS\\Serializer\\Exception\\RuntimeException",... </code></pre> <p>you can see the full <strong>error message <a href="http://d.pr/n/iLLZ" rel="nofollow noreferrer">here</a></strong></p> <p>My current setup is very simple: I have created a single route to a controller that returns a list of products and the price (I followed <a href="http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html" rel="nofollow noreferrer">this example</a> to create the product document). </p> <p>This is the route:</p> <pre><code>rest_product: type: rest resource: Onema\RestApiBundle\Controller\ProductController </code></pre> <p>This is the controller:</p> <pre><code>&lt;?php namespace Onema\RestApiBundle\Controller; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Routing\ClassResourceInterface; use FOS\Rest\Util\Codes; use JMS\Serializer\SerializationContext; use Onema\RestApiBundle\Document\Product; class ProductController extends FOSRestController implements ClassResourceInterface { public function getAction() { $dm = $this-&gt;get('doctrine_mongodb')-&gt;getManager(); $products = $dm-&gt;getRepository('RestApiBundle:Product')-&gt;findAll(); if(!$products) { throw $this-&gt;createNotFoundException('No product found.'); } $data = array('documents' =&gt; $products); $view = $this-&gt;view($data, 200); $view-&gt;setTemplate("RestApiBundle:Product:get.html.twig"); return $this-&gt;handleView($view); } } </code></pre> <p>This is the view called from the controller Resources/Product/get.html.twig:</p> <pre><code>&lt;ul&gt; {% for document in documents %} &lt;li&gt; {{ document.name }}&lt;br /&gt; {{ document.price }} &lt;/li&gt; {% endfor %} &lt;/ul&gt; </code></pre> <p><strong>Any ideas why this would work correctly for one format but not the others? Anything additional I'm supposed to setup?</strong></p> <p><strong>UPDATE:</strong> This is the config values I have been using. At the end of app/config/config.yml I had this:</p> <pre><code>sensio_framework_extra: view: { annotations: false } router: { annotations: true } fos_rest: param_fetcher_listener: true body_listener: true format_listener: true view: formats: json: true failed_validation: HTTP_BAD_REQUEST default_engine: twig view_response_listener: 'force' </code></pre> <p><strong>WORKAROUND:</strong> </p> <p>Doing a bit more research I ran into another error which lead me to this questions and answer:</p> <p><a href="https://stackoverflow.com/a/14030646/155248">https://stackoverflow.com/a/14030646/155248</a></p> <p>Once I got rid of the <code>Doctrine\ODM\MongoDB\LoggableCursor</code> by adding every result to an array like this:</p> <pre><code>$productsQ = $dm-&gt;getRepository('RestApiBundle:Product')-&gt;findAll(); foreach ($productsQ as $product) { $products[] = $product; } return $products; </code></pre> <p>I started getting the results in the correct format. This is kind of a lame solution and still hope to find a better answer to this issue. </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.
 

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