1

たとえば、次のようないくつかの URL クエリがあります。

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

しかし、ご想像のとおり、1 つ以上の変数が空の場合、結果は得られません...

データベースからすべてのアイテムをクエリし、変数でフィルター処理したいのですが、どのようにアプローチすればよいですか?

ご協力いただきありがとうございます!:)

4

1 に答える 1

4

querybuilder 内で findBy-Criteria を作成する必要はないことに注意してください。代わりに、直前に php を使用して作成できます。これarray_filter()により、すべての null 値が削除されます。

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

$item = $this->getDoctrine()
    ->getRepository('AppBundle:Item')
    ->findBy($criteria);
于 2015-02-22T05:18:55.230 に答える