CakePHP 2.3.1 updateAll query not working

余生长醉 提交于 2019-12-11 09:05:36

问题


I am not able to update records in CakePHP 2.3.1

Query:

$this -> Staff -> updateAll(array('Staff.last_login' => date('Y-m-d H:i:s')), array('Staff.id' => $staff['Staff']['id']));

Error:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use


回答1:


See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions :

The $fields array accepts SQL expressions. 
Literal values should be quoted manually using Sanitize::escape().

You can use

NOW()

But, in your case, it would also work with the quoting:

"'" . date('Y-m-d H:i:s') . "'"



回答2:


Try this:

$date = date('Y-m-d H:i:s');
$this->Staff->updateAll(array('Staff.last_login' => "'$date'"), array('Staff.id' => $staff['Staff']['id']));


来源:https://stackoverflow.com/questions/16008714/cakephp-2-3-1-updateall-query-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!