带有多个参数的 Extbase "findBy"

Extbase "findBy" with multiple parameters

我有一个扩展,我想在其中包含一些过滤器,我知道我可以通过使用 findBy.[=15= 来过滤 listAction() 显示的结果]

我测试了它,它是这样工作的:

$cars = $this->carRepository->findByCarid("1");
        $this->view->assign('cars', $cars);

我现在的问题是我需要用多个参数过滤结果,如果我想添加 findByColor("blue") 怎么办,这样它就会给我所有隐藏 1 且颜色为蓝色的汽车? extbase 对这种搜索查询有什么解决方案?我在文档中找不到任何好的或可以理解的东西。

您必须扩展您的存储库并自行编写此功能的代码。 Extbase 为您提供了一个简单但功能强大的 API 方法。

class whatEverYourRepositoryIsCalled extends \TYPO3\CMS\Extbase\Persistence\Repository {
  public function findByFilter($carId, $color) {
    // Create empty query = select * from table
    $query = $this->createQuery();

    // Add query options
    return $query->matching(
      // ALL conditions have to be met (AND)
      $query->logicalAnd(
        // table column carId must be euqal to $carId
        $query->equals('carId', $carId),
        // table column color must be euqal to $color
        $query->equals('color', $color)
      )
    );
  }
}

这是解决您的问题的一种非常简单的方法。在现实世界的场景中,我可能会使用一组过滤条件来进行过滤,例如 array('carId' => 1, 'color' => 'blue')。在 findByFilter() 中,这些值将被提取并添加到查询中。

关键是构建所需的查询。在 http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/ 中可以找到有关如何执行此操作的非常全面的说明。不幸的是,它不是完全最新的,但关于构建查询的部分仍然有效。