簡體   English   中英

Symfony2 /主義:findBy多個查詢可以為NULL

[英]Symfony2 / Doctrine: findBy multiple queries that can be NULL

我有幾個網址查詢,例如:

/page?type=train&category=others&location=germany

/page?type=car&category=others

我獲取它們並將它們放入要用來過濾數據庫請求的變量中。

那就是我嘗試過的:

$item = $this->getDoctrine()
        ->getRepository('AppBundle:Item')
        ->findBy(array(
            'type' => $type,
            'category' => $category,
            'location' => $location
          ));

但是正如您可以想象的,如果一個或多個變量為空,那么我沒有任何結果...

我想從數據庫中查詢所有項目並按變量過濾它們,我該如何處理?

謝謝你的幫助! :)

請記住,您不必在querybuilder中創建findBy-Criteria。 相反,您可以在之前使用php的array_filter()創建它,它將刪除所有空值:

$criteria = array_filter(array(
  'type' => 'search_type',
  'category' => null,
  'location' => 'search_location'
));

$item = $this->getDoctrine()
    ->getRepository('AppBundle:Item')
    ->findBy($criteria);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM