gpt4 book ai didi

php - Symfony2/学说 : findBy multiple queries that can be NULL

转载 作者:行者123 更新时间:2023-11-29 04:40:56 26 4
gpt4 key购买 nike

我有几个 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
));

但是您可以想象,如果一个或多个变量为空,我将得不到任何结果...

我想查询数据库中的所有项目并按变量过滤它们,我该如何处理?

感谢您的帮助! :)

最佳答案

请记住,您不必在查询生成器中创建 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);

关于php - Symfony2/学说 : findBy multiple queries that can be NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653202/

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