首頁>Web>source

我正在drupal 8 php中構建一个REST資源,以將查詢結果返迴到資料庫.但是使用 fetchAssoc() 仅返迴一个結果。 所以我想起了 返迴所有結果,但是這樣做会引發錯誤。 這是代碼

fetchAll()

這是錯誤

$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);

有什麼想法吗?

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 &quot;stdClass&quot;, no supporting normalizer found. in <em class="placeholder">Symfony\Component\Serializer\Serializer-&gt;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-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 116) Symfony\Component\Serializer\Serializer-&gt;serialize(Array, &#039;json&#039;, Array) (Line: 176) Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&gt;Drupal\rest\EventSubscriber\{closure}() (Line: 573) Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 177) Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&gt;renderResponseBody(Object, Object, Object, &#039;json&#039;) (Line: 76) Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&gt;onResponse(Object, &#039;kernel.response&#039;, Object) call_user_func(Array, Object, &#039;kernel.response&#039;, Object) (Line: 111) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch(&#039;kernel.response&#039;, Object) (Line: 191) Symfony\Component\HttpKernel\HttpKernel-&gt;filterResponse(Object, Object, 1) (Line: 173) Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68) Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57) Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 106) Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 85) Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52) Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 708) Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19) </pre>
最新回復
  • 5月前
    1 #

    fetchAssoc對您有用,因為結果是一个關聯陣列。

    fetchAll不起作用,因為結果是一个stdClass物件陣列,並且JSON規範化器不知道如何返迴資料。

    我认為您將需要fetchAllKeyed(0,0),但更好地發挥作用。

    參考:https://www.drupal.org/docs/8/api/database-api/result-sets

  • apache:Mod重寫多个查詢字元串
  • google analytics:我可以通過隱藏的鏈接將utm跟蹤引數添加到会員點击吗?