. 我测试了它,它是这样工作的: $cars = $this->carRepos-6ren">
gpt4 book ai didi

typo3 - 带有多个参数的 Extbase "findBy"

转载 作者:行者123 更新时间:2023-12-04 02:14:13 27 4
gpt4 key购买 nike

我有一个扩展,我想在其中包含一些过滤器,我知道我可以使用 findBy 过滤我的 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/ 找到。 .不幸的是,它不是完全最新的,但关于构建查询的部分仍然有效。

关于typo3 - 带有多个参数的 Extbase "findBy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462807/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com