【问题标题】:Save without updating the 'modified' field保存而不更新“修改”字段
【发布时间】:2015-04-01 07:58:30
【问题描述】:

我正在使用 CakePHP 3.0 开发一个新项目。

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

UsersController:

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;

它不起作用,抛出和错误:Call to a member function format() on a non-object 因为我猜日期时间字段现在被视为对象。

任何帮助将不胜感激,

保罗

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

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

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

    你也可以像以前一样做,但你需要在保存之前禁用时间戳行为:

    $this->behaviors()->unload('Timestamp');
    

    我会推荐使用updateAll

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2021-02-09
      相关资源
      最近更新 更多