user3320954
user3320954

Reputation: 15

Doctrine DBAL increment

how can I increment a value?

$app->db->update('videos', array(
    'views' => 'views + 1'
), array(
    'id' => $id
));

It doesn't work in many ways I tried.

Upvotes: 1

Views: 1514

Answers (2)

Kinobi
Kinobi

Reputation: 192

A better approach could be to use the method executeUpdate()

$app->db->executeUpdate("UPDATE videos SET views=views+1 WHERE id=?", array($id));

Edit 2022

The API is working but marked deprecated. Use executeStatement() instead of executeUpdate()

Upvotes: 4

common sense
common sense

Reputation: 3912

You have to ask the db for the current value of views first, then increment the value and save the new value into db.

$result = $app->db->fetchAssoc('SELECT views FROM videos WHERE id = ?', array($id));
$result['views']++;
$app->db->update('videos', array('views' => $result['views']), array('id' => $id));

Upvotes: -1

Related Questions