Answer the question
In order to leave comments, you need to log in
What's wrong with the update request?
yii2, can't do multiple update (~10k records need to be updated).
the request looks like this:
UPDATE `product`
SET `name`=(
CASE
WHEN `code`=122 THEN "product_name1"
/*...*/
WHEN `code`=125 THEN "product_nameN"
END)
WHERE `code` IN (122,125,/*...*/, 999999);
$sql = ''
. 'UPDATE {{' . Products::tableName() . '}}'
. ' SET =(CASE';
foreach ($name_update_array as $record_code => $record_name) {
$sql .= ' WHEN =' . $record_code . ' THEN "' . $record_name . '"';
}
$sql .= ' END) WHERE IN (' . implode(',', array_keys($name_update_array)) . ')';
$query = \Yii::$app->db->createCommand($sql);
try {
$query->execute();
}
catch (\Exception $exception) {
Log::addLog([
'logfile' => $this->logfile,
'name' => $settings['name'],
'status' => 'ERROR',
'data' => $exception
]);
return false;
}
Next yii\db\Exception: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
UPDATE product SET name=(CASE WHEN code=121 THEN "qwe" WHEN code=122 THEN "qweasd" END) WHERE code IN (121,122,125,126,127);
Query OK, 5 rows affected, 3 warnings (0.03 sec)
Rows matched: 5 Changed: 5 Warnings: 3
Answer the question
In order to leave comments, you need to log in
we part.
was passed a json curve, where there was a name with quotes, and when I tried to make a request, it came out something like this:
fixed it like this:
$sql .= ' WHEN =' . $record_code . ' THEN \'' . $record_name . '\'';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question