diff --git a/system/Model.php b/system/Model.php index 83c62bad1f47..934dc0eb1206 100644 --- a/system/Model.php +++ b/system/Model.php @@ -86,7 +86,14 @@ class Model * @var string */ protected $primaryKey = 'id'; - + + /** + * Last insert ID + * + * @var int + */ + protected $insertID = 0; + /** * The Database connection group that * should be instantiated. @@ -539,6 +546,18 @@ public static function classToArray($data, string $dateFormat = 'datetime'): arr //-------------------------------------------------------------------- + /** + * Returns last insert ID or 0. + * + * @return int + */ + public function getInsertID() + { + return $this->insertID; + } + + //-------------------------------------------------------------------- + /** * Inserts data into the current table. If an object is provided, * it will attempt to convert it to an array. @@ -551,6 +570,8 @@ public static function classToArray($data, string $dateFormat = 'datetime'): arr public function insert($data = null, bool $returnID = true) { $escape = null; + + $this->insertID = 0; if (empty($data)) { @@ -620,8 +641,10 @@ public function insert($data = null, bool $returnID = true) return $result; } + $this->insertID = $this->db->insertID(); + // otherwise return the insertID, if requested. - return $returnID ? $this->db->insertID() : $result; + return $returnID ? $this->insertID : $result; } //--------------------------------------------------------------------