I’m building a REST Resource in drupal 8 php that returns the result of a query to the database. However using the fetchAssoc()
only returns one result. So I figured that fetchAll()
returns all results, but it throws an error when I do so. This is the code
$result = $database ->select('node__field_recipe_category', 'recipe_cat') ->fields('recipe_cat', ['entity_id','bundle']) ->condition('entity_id', [3,8], 'BETWEEN') ->execute() ->fetchAll(); // Configure caching settings. $build = [ '#cache' => [ 'max-age' => 0, ], ]; // Return results. return (new ResourceResponse($result, 200))->addCacheableDependency($build);
This is the error
The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">SymfonyComponentSerializerExceptionNotNormalizableValueException</em>: Could not normalize object of type "stdClass", no supporting normalizer found. in <em class="placeholder">SymfonyComponentSerializerSerializer->normalize()</em> (line <em class="placeholder">164</em> of <em class="placeholder">vendor/symfony/serializer/Serializer.php</em>). <pre class="backtrace">SymfonyComponentSerializerSerializer->normalize(Array, 'json', Array) (Line: 116) SymfonyComponentSerializerSerializer->serialize(Array, 'json', Array) (Line: 176) DrupalrestEventSubscriberResourceResponseSubscriber->DrupalrestEventSubscriber{closure}() (Line: 573) DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 177) DrupalrestEventSubscriberResourceResponseSubscriber->renderResponseBody(Object, Object, Object, 'json') (Line: 76) DrupalrestEventSubscriberResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object) call_user_func(Array, Object, 'kernel.response', Object) (Line: 111) DrupalComponentEventDispatcherContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191) SymfonyComponentHttpKernelHttpKernel->filterResponse(Object, Object, 1) (Line: 173) SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Line: 68) SymfonyComponentHttpKernelHttpKernel->handle(Object, 1, 1) (Line: 57) DrupalCoreStackMiddlewareSession->handle(Object, 1, 1) (Line: 47) DrupalCoreStackMiddlewareKernelPreHandle->handle(Object, 1, 1) (Line: 106) Drupalpage_cacheStackMiddlewarePageCache->pass(Object, 1, 1) (Line: 85) Drupalpage_cacheStackMiddlewarePageCache->handle(Object, 1, 1) (Line: 47) DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52) DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object, 1, 1) (Line: 23) StackStackedHttpKernel->handle(Object, 1, 1) (Line: 708) DrupalCoreDrupalKernel->handle(Object) (Line: 19) </pre>
Any ideas?