Cakephp 保存而不更新';修改';领域
我正在使用CakePHP3.0进行一个新项目 我正在使用身份验证组件,每当用户登录时,我都会更新字段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);
的值
用户控制器:
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]);