fetchAll Database query returns an error 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
      -&gt;select('node__field_recipe_category', 'recipe_cat')
      -&gt;fields('recipe_cat', ['entity_id','bundle'])
      -&gt;condition('entity_id', [3,8], 'BETWEEN')
      -&gt;execute()
      -&gt;fetchAll();
     // Configure caching settings.
    $build = [
      '#cache' =&gt; [
        'max-age' =&gt; 0,
      ],
    ];
    
    // Return results.
    return (new ResourceResponse($result, 200))-&gt;addCacheableDependency($build);

This is the error
The website encountered an unexpected error. Please try again later.&lt;/br&gt;&lt;/br&gt;&lt;em class=&quot;placeholder&quot;&gt;Symfony\Component\Serializer\Exception\NotNormalizableValueException&lt;/em&gt;: Could not normalize object of type &amp;quot;stdClass&amp;quot;, no supporting normalizer found. in &lt;em class=&quot;placeholder&quot;&gt;Symfony\Component\Serializer\Serializer-&amp;gt;normalize()&lt;/em&gt; (line &lt;em class=&quot;placeholder&quot;&gt;164&lt;/em&gt; of &lt;em class=&quot;placeholder&quot;&gt;vendor/symfony/serializer/Serializer.php&lt;/em&gt;). &lt;pre class=&quot;backtrace&quot;&gt;Symfony\Component\Serializer\Serializer-&amp;gt;normalize(Array, &amp;#039;json&amp;#039;, Array) (Line: 116)
Symfony\Component\Serializer\Serializer-&amp;gt;serialize(Array, &amp;#039;json&amp;#039;, Array) (Line: 176)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&amp;gt;Drupal\rest\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer-&amp;gt;executeInRenderContext(Object, Object) (Line: 177)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&amp;gt;renderResponseBody(Object, Object, Object, &amp;#039;json&amp;#039;) (Line: 76)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&amp;gt;onResponse(Object, &amp;#039;kernel.response&amp;#039;, Object)
call_user_func(Array, Object, &amp;#039;kernel.response&amp;#039;, Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&amp;gt;dispatch(&amp;#039;kernel.response&amp;#039;, Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel-&amp;gt;filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel-&amp;gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&amp;gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&amp;gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&amp;gt;handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache-&amp;gt;pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache-&amp;gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&amp;gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&amp;gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&amp;gt;handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel-&amp;gt;handle(Object) (Line: 19)
&lt;/pre&gt;

Any ideas?
fetchAssoc worked for you because of the result is an associative array.
fetchAll did not work because the result is an array of stdClass objects and JSON normalizer did not know how to return the data.
I think you will need fetchAllKeyed(0,0), but better play around a bit.
reference: https://www.drupal.org/docs/8/api/database-api/result-sets