php - CakePHP 2.8.4 : Change User balance

标签 php mysql cakephp

我想更改我的表用户中的帐户余额。但我想在另一个 Controller 而不是 UsersController 中执行此操作。

M代码:

	public function myfunctionname() {
    $this->layout = 'customer';
    $this->loadModel('User');

    if ($this->request->is('post')) {
      // Some working stuff
				$this->User->updateAll(array('balance' => 'balance - 5'), array('customerid' => $this->Session->read('Auth.User.id')));
        $this->MyControllerName->create();
        if ($this->MyControllerName->save($this->request->data)) {
          $this->Flash->success( __('Success!'));
          $this->redirect('some redirect');
        } else {
          $this->Flash->error( __('Error!'));
        }
    }
	}

我会收到以下错误:

Database Error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customerid' in 'where clause'

SQL Query: UPDATE `mydbname`.`users` AS `User` SET `User`.`balance` = balance - 5 WHERE `customerid` = '5' 

谢谢!

最佳答案

我明白了:

$this->User->updateAll(array('balance' => 'balance - 5'), array('id' => $this->Session->read('Auth.User.id')));

关于php - CakePHP 2.8.4 : Change User balance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811044/

相关文章:

php - 阻止上传可执行图像 (PHP)

PHP/Phinx - 插入经度/纬度导致 PDO MySQL 几何对象错误

php - 如何在两个不同的列中分别插入下拉值和下拉名称

mysql - ActiveRecord 的分布式事务边界

apache - .htaccess 条件 RewriteBase

php - 在插件元素 CakePHP 3 中获取当前插件名称

php - 在 php 中, "echo ` 命令` "做什么?(带单引号)

php - 在 PHP 中访问 stdClass 中第一个属性的属性名称

php - MySQL递归树搜索

php - 无法使用 cakePHP 写入数据库