Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 保存而不更新';修改';领域_Cakephp_Cakephp 3.0 - Fatal编程技术网

Cakephp 保存而不更新';修改';领域

Cakephp 保存而不更新';修改';领域,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我正在使用CakePHP3.0进行一个新项目 我正在使用身份验证组件,每当用户登录时,我都会更新字段的值 用户控制器: public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user);

我正在使用CakePHP3.0进行一个新项目

我正在使用身份验证组件,每当用户登录时,我都会更新字段
的值

用户控制器:

public function login() {

    if ($this->request->is('post')) {

        $user = $this->Auth->identify();

        if ($user) {

            $this->Auth->setUser($user);

            $this->Users->setVisited($user['id']);

            return $this->redirect($this->Auth->redirectUrl());

        }

    $this->Flash->error('Your username or password is incorrect.');

    }

}
public function setVisited($id) {

    $user = $this->findById($id)->first();

    $user->visited = Time::now();

    if($this->save($user)) {

        return true;

    }

    return false;

}
UsersTable:

public function login() {

    if ($this->request->is('post')) {

        $user = $this->Auth->identify();

        if ($user) {

            $this->Auth->setUser($user);

            $this->Users->setVisited($user['id']);

            return $this->redirect($this->Auth->redirectUrl());

        }

    $this->Flash->error('Your username or password is incorrect.');

    }

}
public function setVisited($id) {

    $user = $this->findById($id)->first();

    $user->visited = Time::now();

    if($this->save($user)) {

        return true;

    }

    return false;

}
现在,我想在不更新字段
modified
的值的情况下进行此保存。我尝试过cake以前版本中使用的方法:

$user->modified = false;
但它不起作用,抛出并出错:
调用非对象上的成员函数format()
,因为datetime字段现在被视为对象

任何帮助都将不胜感激


保罗

你有几种方法可以做到这一点。实际上,您希望在保存实体时避免调用回调。对于这些情况,您有
updateAll

$this->updateAll(['visited' => Time::now()], ['id' => $id]);
您也可以执行与以前相同的操作,但在保存之前需要禁用时间戳行为:

$this->behaviors()->unload('Timestamp');
我建议使用
updateAll

$this->updateAll(['visited' => Time::now()], ['id' => $id]);