简体   繁体   中英

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. For example, if you need to find entities with width: 195,245 you can pass the data like this

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

For removing the empty params you can use array_filter function

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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