CodeIgniter Forums
Model return id record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Model return id record (/showthread.php?tid=73965)



Model return id record - Nome - 06-30-2019

What are the ways to return the id of the created record in the table?

PHP Code:
  public function insertAny($title$slug) {
    $this->table('node')->protect(false)->place([
      'title'    => $title,
      'slug'     => $slug
    
])
    return ID of record? ;
  

Because I have only one idea, this is to create an additional unique field on which, after executing the record, perform a search and return the ID. But I think there should be some solution out of the CI box.

P.S
Thank for your attention!


RE: Model return id record - InsiteFX - 06-30-2019

CodeIgniter 4 User Guide - Query Helper Methods


PHP Code:
$db->insertID() 



RE: Model return id record - MGatner - 07-02-2019

What @InsiteFX said. Also if you do your insert through a Model you can use the method `getInsertID()` that will return the last insert handle *by that model*. This can also be returned directly from the insert statement using the second parameter: `$insertID = $model->insert($row, true)`