1

I have a query that looks like this :

$cleanupQuery = $conn->prepare("
  DELETE FROM tbl_annotations
  WHERE
    id IN (:removedAnnotations)
");

Obviously, the point is to end up with this kind of query

DELETE FROM tbl_annotations WHERE id IN (1, 2, 3, 4)

Is this possible with Zend Framework 2?

If not, is there a solution for this problem?


Update

Despite some people's decision to make this question as a duplicate, I have chosen an alternate solution : FIND_IN_SET().

$cleanupQuery = $conn->prepare("
  DELETE FROM tbl_annotations
  WHERE
    FIND_IN_SET(id, :removedAnnotations)
");

And I have removedAnnotations set via implode(',', $annotationIds). These ids are already filtered through a validation function, so I know they are all integers.

This works in my case, but it would certainly break when using direct input from the user.

Do not use this method unless all array values are guaranteed to be sanitized.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • Further to the answers in the duplicate question, you may also wish to consider Zend Framework's SQL abstraction wherein you can build a `WHERE` predicate using the [`in()` or `notIn()`](https://docs.zendframework.com/zend-db/sql/#inidentifier-valueset-notinidentifier-valueset) functions with either an array or another `Select` object as the `$valueSet` argument. – eggyal Mar 25 '18 at 10:15
  • That FIND_IN_SET solution is already mentioned in the target duplicate. No need to mention it here as well, the fact that your question is closed as a duplicate of the other one is sufficient. We did not "make" this question a duplicate, it is a duplicate by virtue of being a duplicate. That is, fully answered in another question. – Félix Adriyel Gagnon-Grenier Mar 27 '18 at 11:17

0 Answers0