在cakepp中获取下一个自动递增id


get the next auto increment id in cakephp

我想在appModel中编写一个函数,为我想要的每个模型返回下一个自动递增的主id。例如,在UserController中,我使用$this->user->nextId()。我知道返回下一个自动递增主id:的查询

SELECT Auto_increment FROM information_schema.tables WHERE table_name='table-name' AND table_schema='db-name'

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-查询

所以你可以这样做:

public function nextId() {
    $result = $this->User->query("SELECT Auto_increment FROM information_schema.tables AS NextId  WHERE table_name='table-name' AND table_schema='db-name'");
    return $result[0]['NextId']['Auto_increment'];
}

您可能需要编辑返回语句。我不确定这是否是它将使用的确切格式。

不过,正如其他人所说,这不是你应该做的事情。