简体   繁体   English

如何将数组传递给 doctrine 查询 findBy?

[英]How to pass array into the doctrine query findBy?

Before I had filter params like below:在我有如下过滤器参数之前:

width: 195
diameter: 15
load: 91

So, I simply use following query to retrieve result.所以,我只是使用以下查询来检索结果。

$list = $this->entityManager->getRepository(List::class)->findBy($filterArray);

Now, my requirement is changed.现在,我的要求改变了。 I can pass comma-separated string我可以传递逗号分隔的字符串

width: 195,245
diameter: 15
load: 91

So, first I convert this string into array:所以,首先我将此字符串转换为数组:

[
  "width" => [
    0 => "195"
    1 => "245"
  ]
  "diameter" => [
    0 => "15"
  ]
  "load" => [
    0 => "91"
  ]
]

Now I am not sure how to make a query.现在我不确定如何进行查询。 Filter param can vary.过滤器参数可以变化。 Its not necessary that all the param are passed.没有必要传递所有参数。

With findBy function you can filter with an array param.使用findBy function 您可以使用数组参数进行过滤。 For example, if you need to find entities with width: 195,245 you can pass the data like this例如,如果您需要查找width: 195,245的实体,您可以像这样传递数据

['width' => [195, 245]]

For removing the empty params you can use array_filter function要删除空参数,您可以使用array_filter

$list = $this->entityManager->getRepository(List::class)->findBy(array_filter($filterArray));

Later if your params will grow, you can use the Doctrine QB for filtering your data稍后如果您的参数会增长,您可以使用 Doctrine QB 过滤您的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM