Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to model's last inserted ID #1440

Merged
merged 1 commit into from
Nov 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

@atefBB atefBB Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this feature (returning inserted row ID with $returnID flag) isn't well documented! Need to be documented.

{
$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