Yii2 DB functions are based on PDO . According to the bindValue manual bindValue no support for values ββfrom the Array type. (The third parameter is data_type ).
The solution is to create a string before the query that matches your IN clause and binds it as a string.
Something like:
$parents = "1,2,3"; $sql = $this->db->createCommand("UPDATE some_table SET something='foo' WHERE some_id IN (:parents)"); $sql->bindValue(':parents', $parents);
Edit
It seems that the placeholder is replaced by the impotent array as a single string value of '1,2,3' instead of '1','2','3' (since it is the only placeholder).
To solve this problem, I suggest using multiple placeholders ? . Therefore, instead of IN (:parents) , you will have IN (?, ?, ?, ? ,....) , and since we already have an organized array, we can use count($array) to find out how many placeholders we have need to put.
//$parents = array(1,2,3); $placeholders = str_repeat('?,', count($parents) - 1). '?'; $sql = $this->db->createCommand("UPDATE some_table SET something='foo' WHERE some_id IN (".$placeholders.")"); foreach($parents as $i => $parent){ $sql->bindValue($i+1, $parent); }
Pay attention to the passed value of the first parameter bindValue; The reason it is $i+1 and not $i is listed in the manual:
For a prepared statement using question mark placeholders, this will be the 1-indexed parameter position .
For more information and alternative solutions, consider the following answer: stack overflow
Ofir baruch
source share