4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【CakePHP】insertされたレコードのプライマリキーを取得する

Last updated at Posted at 2015-04-08

取得方法

$this->getLastInsertID();

もしくは、

$this->getInsertID();

でinsertされたレコードのプライマリキー(デフォではid、プライマリキーを変更していれば変更後のカラム)を取得できる。

取得例
$this->save($data);
$lastInsertID = $this->getLastInsertID();

ちなみに、getLastInsertID()はgetInsertID()をラップしているだけ。たぶんどっちを使ってもいいけど、名前が直感的だからgetLastInsertID()をよく使う。

以下は、lib/Cake/Model/Model.phpのgetLastInsertIDメソッド。

public function getLastInsertID() {
  return $this->getInsertID();
}
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?