我正在使用cakephp 3.在控制器中我正在使用此代码:
$conn = ConnectionManager::get('default');
$stmt = $conn->execute('UPDATE posts SET views = views + 1 WHERE id = '.$id.';');
是否有可能将此声明写成“cakephp-like”?
答案 0 :(得分:2)
有可能:
$posts = TableRegistry::get('Posts');
$query = $posts->query();
$query->update()
->set($query->newExpr('views = views + 1'))
->where(['id' => $id])
->execute();