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">Symfony\Component\Serializer\Exception\NotNormalizableValueException</em>: Could not normalize object of type "stdClass", no supporting normalizer found. in <em class="placeholder">Symfony\Component\Serializer\Serializer->normalize()</em> (line <em class="placeholder">164</em> of <em class="placeholder">vendor/symfony/serializer/Serializer.php</em>). <pre class="backtrace">Symfony\Component\Serializer\Serializer->normalize(Array, 'json', Array) (Line: 116)
Symfony\Component\Serializer\Serializer->serialize(Array, 'json', Array) (Line: 176)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber->Drupal\rest\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 177)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object, Object, Object, 'json') (Line: 76)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
</pre>
Any ideas?