Skip to content

Commit 0dc00bd

Browse files
committed
Add static method indexBy()
1 parent 7e75033 commit 0dc00bd

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

README.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,30 @@ This package provide Base Model which extended `CI_Model` and provided full CRUD
2828
OUTLINE
2929
-------
3030

31-
* [Demonstration](#demonstration)
32-
33-
* [Requirements](#requirements)
34-
35-
* [Installation](#installation)
36-
37-
* [Configuration](#configuration)
38-
39-
* [Defining Models](#defining-models)
31+
- [Demonstration](#demonstration)
32+
- [Requirements](#requirements)
33+
- [Installation](#installation)
34+
- [Configuration](#configuration)
35+
- [Defining Models](#defining-models)
4036
- [Table Names](#table-names)
4137
- [Primary Keys](#primary-keys)
4238
- [Timestamps](#timestamps)
4339
- [Database Connection](#database-connection)
44-
45-
* [Usage](#usage)
40+
- [Usage](#usage)
4641
- [find()](#find)
4742
- [insert()](#insert)
4843
- [update()](#update)
4944
- [delete()](#delete)
50-
51-
* [Soft Deleted](#soft-deleted)
45+
- [Soft Deleted](#soft-deleted)
5246
- [Configuration](#configuration-1)
5347
- [Usage](#usage-1)
54-
55-
* [Query Scopes](#query-scopes)
48+
- [Query Scopes](#query-scopes)
5649
- [Configuration](#configuration-2)
5750
- [Usage](#usage-2)
58-
59-
* [Read & Write Connections](#read--write-connections)
51+
- [Read & Write Connections](#read--write-connections)
6052
- [Configuration](#configuration-3)
6153
- [Load Balancing for Databases](#load-balancing-for-databases)
62-
63-
* [Pessimistic Locking](#pessimistic-locking)
54+
- [Pessimistic Locking](#pessimistic-locking)
6455

6556
---
6657

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"source": "https://github.com/yidas/codeigniter-model"
1111
},
1212
"minimum-stability": "stable",
13+
"require": {
14+
"php": ">=5.4.0"
15+
},
1316
"autoload": {
1417
"classmap": ["src/"]
1518
}

example/requestLog/My_model.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class My_model extends yidas\Model
2323

2424
// Mainstream creating field name
2525
const CREATED_AT = 'created_at';
26+
27+
// Log has no updating
28+
const UPDATED_AT = null;
2629

2730
protected $timestamps = true;
2831

example/requestLog/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CREATE TABLE `table` (
1515
`id` bigint(20) UNSIGNED NOT NULL,
1616
`ip` char(15) DEFAULT NULL COMMENT 'IP header',
1717
`user_agent` varchar(255) DEFAULT NULL COMMENT 'User-Agent header',
18-
`c_date` datetime NOT NULL
18+
`created_at` datetime NOT NULL
1919
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
2020

2121
ALTER TABLE `table`

src/Model.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,37 @@ public function withAll()
656656
return $this;
657657
}
658658

659+
/**
660+
* Index by Key
661+
*
662+
* @param array $array Array data for handling
663+
* @param string $key Array key for index key
664+
* @param bool $obj2Array Object converts to array if is object
665+
* @return array Result with indexBy Key
666+
* @example
667+
* $records = $this->Model->findAll();
668+
* $this->Model->indexBy($records, 'sn');
669+
*/
670+
public static function indexBy(Array &$array, $key=null, $obj2Array=false)
671+
{
672+
// Use model instance's primary key while no given key
673+
$key = ($key) ?: (new static())->primaryKey;
674+
675+
$tmp = [];
676+
foreach ($array as $row) {
677+
// Array & Object types support
678+
if (is_object($row) && isset($row->$key)) {
679+
680+
$tmp[$row->$key] = ($obj2Array) ? (array)$row : $row;
681+
}
682+
elseif (is_array($row) && isset($row[$key])) {
683+
684+
$tmp[$row[$key]] = $row;
685+
}
686+
}
687+
return $array = $tmp;
688+
}
689+
659690
/**
660691
* Query Scopes Handler
661692
*

0 commit comments

Comments
 (0)