Magento 2 Database – How to Update Query Using Standard Way

customdatabasemagento2modulequery

I have UPDATE posts SET viewed = viewed + 1 WHERE id = 1 MySql Query

I have referred https://webkul.com/blog/magento2-write-custom-mysql-query-without-using-model/
But i want to as we do Select Way, Means Standard Way

Example:

$select = $this->getConnection()
                ->select()
                ->from(['posts' => $this->getMainTable()])
                ->where(
                'posts.id = ?', $id);

Best Answer

Try using:

$this->getConnection()->update(
    'posts',
    [
        'viewed' => new \Zend_Db_Expr('viewed + 1'),
    ],
    ["id = ?" => 1]
);