我需要为列生成唯一值。我想增加表中最后插入的值。
我试过这个,但无济于事
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'));
}
}
谢谢!
答案 0 :(得分:3)
您正在保存 $ this-> request-> data ,但您将数据存入 $ this->数据。
试试这个:
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'));
}
}
答案 1 :(得分:0)
$ id = $ this-> User-> getLastInsertId();
通过此函数,您将获得最后一个插入的id,现在递增其值并在您想要使用的位置使用它或者将该id添加到数据库中并将其保存在数据库中