R
R
Ruslan Ruslanov2019-06-06 10:37:23
Yii
Ruslan Ruslanov, 2019-06-06 10:37:23

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);

php code:
$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;
}

mistake:
Next yii\db\Exception: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

at manual input and on small amounts of records everything perfectly fulfills.
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

how to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Ruslanov, 2019-06-06
@dasauser

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 question

Ask a Question

731 491 924 answers to any question