SkysTheLimit_81 SkysTheLimit_81 - 3 months ago 12x
SQL Question

How to count views of posts with CakePHP3?

I am using cakephp 3. In the controller I am using this code:

$conn = ConnectionManager::get('default');
$stmt = $conn->execute('UPDATE posts SET views = views + 1 WHERE id = '.$id.';');

is there a possibility to write this statement "cakephp-like"?


There is a possibility:

$posts = TableRegistry::get('Posts');
$query = $posts->query();
    ->set($query->newExpr('views = views + 1'))
    ->where(['id' => $id])