Skip to content

Commit

Permalink
Merge pull request #1440 from nowackipawel/patch-17
Browse files Browse the repository at this point in the history
Access to model's last inserted ID
  • Loading branch information
jim-parry authored Nov 11, 2018
2 parents a0cdb73 + c3be682 commit eaa8487
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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))
{
Expand Down Expand Up @@ -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;
}

//--------------------------------------------------------------------
Expand Down

0 comments on commit eaa8487

Please sign in to comment.