Cannot convert value of type string to integer

I would need to update the number of pieces by bulk update. But I don’t know how to put the right variable there. Please, give me an advice.

$this->GoodsItems->updateAll([
    'stock' => 'stock' - $item->pcs,
], [
    'id' => $item->goods_item_id,
]);

Column stock is integer

There’s various ways to do that. With dynamic values involved, personally I’d suggest to create the update query manually, so that you can safely bind the values:

$statement = $this->GoodsItems
    ->query()
    ->update()
    ->set(['stock = stock - :pcs'])
    ->bind(':pcs', $item->pcs, 'integer')
    ->where(['id' => $item->goods_item_id])
    ->execute();

$statement->closeCursor();
$affected = $statement->rowCount();
2 Likes