增量最后插入值CakePHP
我需要为列生成唯一的值。我想增加表中最后插入的值 我试过了,但是没有用增量最后插入值CakePHP,cakephp,Cakephp,我需要为列生成唯一的值。我想增加表中最后插入的值 我试过了,但是没有用 if($this->request->is('post')){ $code = $this->Department->find('first', array( 'fields' => 'Department.code', 'order' => 'Department.code DESC' )); $code += 1; $t
if($this->request->is('post')){
$code = $this->Department->find('first', array(
'fields' => 'Department.code',
'order' => 'Department.code DESC'
));
$code += 1;
$this->data['Department']['code'] = $code;
if($this->Department->save($this->request->data)){
$this->Session->setFlash('New department added.');
$this->redirect(array('action' => 'add'));
}
}
谢谢 您正在保存$this->request->data但您将数据放入$this->data中 试试这个:
if($this->request->is('post')){
$code = $this->Department->find('first', array('fields' => 'Department.code','order' => 'Department.code DESC'));
$code += 1;
$this->request->data['Department']['code'] = $code;
if($this->Department->save($this->request->data)){
$this->Session->setFlash('New department added.');
$this->redirect(array('action' => 'add'));
}
}
$id=$this->User->getLastInsertId()
通过此函数,您将获得最后一个插入的id,现在增加它的值并在您想要使用的地方使用它,或者增加此id并将其保存在数据库中您也可以使用atomic updateAll()在单个快速查询中完成所有操作-请参阅。