Skip to content

Commit b6d2c1e

Browse files
committed
Add method getLastInsertID()
1 parent 784ad5e commit b6d2c1e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ OUTLINE
3939
- [Database Connection](#database-connection)
4040
- [Usage](#usage)
4141
- [find()](#find)
42-
- [insert()](#insert)
42+
- [findOne()](#findone)
43+
- [findAll()](#findall)
44+
- [insert()](#insert))
45+
- [batchInsert()](#batchinsert)
4346
- [update()](#update)
47+
- [replace()](#replace)
4448
- [delete()](#delete)
49+
- [getLastInsertID()](#getlastinsertid))
4550
- [Soft Deleted](#soft-deleted)
4651
- [Configuration](#configuration-1)
4752
- [Usage](#usage-1)
@@ -52,6 +57,8 @@ OUTLINE
5257
- [Configuration](#configuration-3)
5358
- [Load Balancing for Databases](#load-balancing-for-databases)
5459
- [Pessimistic Locking](#pessimistic-locking)
60+
- [Helpers](#helpers)
61+
- [indexBy()](#indexby)
5562

5663
---
5764

@@ -390,6 +397,14 @@ $result = $this->Model->delete();
390397
$this->Model->delete(123, true);
391398
```
392399

400+
### getLastInsertID()
401+
402+
Get the insert ID number when performing database inserts.
403+
404+
```php
405+
$result = $this->Model->insert(['name' => 'Nick Tsai']);
406+
$lastInsertID = $this->Model->getLastInsertID();
407+
```
393408

394409
---
395410

@@ -643,3 +658,30 @@ $this->Model->find()->where('id', 123)
643658
$result = $this->Model->lockForUpdate()->row_array();
644659
$this->Model->getDB()->trans_complete();
645660
```
661+
662+
---
663+
664+
HELPERS
665+
-------
666+
667+
### `indexBy()`
668+
669+
Index by Key
670+
671+
```php
672+
array indexBy(Array & $array [, Integer $key] [, Boolean $obj2Array])
673+
```
674+
675+
Example:
676+
677+
```php
678+
$records = $this->Model->findAll();
679+
$this->Model->indexBy($records, 'sn');
680+
681+
// Result example of $records:
682+
[
683+
7 => ['sn'=>7, title=>'Foo'],
684+
13 => ['sn'=>13, title=>'Bar']
685+
]
686+
```
687+

src/Model.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ public function batchInsert($data)
418418
return $this->_db->insert_batch($this->table, $data);
419419
}
420420

421+
/**
422+
* Get the insert ID number when performing database inserts.
423+
*
424+
* @return integer Last insert ID
425+
*/
426+
public function getLastInsertID()
427+
{
428+
return $this->getDB()->insert_id();
429+
}
430+
421431
/**
422432
* Replace a row with Timestamps feature into the associated database table using the attribute values of this record.
423433
*

0 commit comments

Comments
 (0)