1

我需要为列生成唯一值。我想增加表中最后插入的值。

我试过这个,但无济于事

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'));
    }
}

谢谢!

4

2 回答 2

3

You are saving $this->request->data but you put your data in $this->data.

Try with 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'));
    }
}
于 2012-09-20T08:36:44.720 回答
0

$id = $this->User->getLastInsertId ();

通过这个函数,您将获得最后插入的 id,现在增加它的值并在您想要使用的地方使用它或增加这个 id 并将其保存在您的数据库中

于 2012-09-20T12:22:16.680 回答