Skip to content

Commit 21e4b1b

Browse files
committed
Release version 2.0.0
- Apply PSR-4 autoload method for lib namespace
1 parent 7c71c09 commit 21e4b1b

File tree

5 files changed

+102
-57
lines changed

5 files changed

+102
-57
lines changed

README.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ OUTLINE
6262
DEMONSTRATION
6363
-------------
6464

65-
### Find one
65+
### Find One
6666
```php
6767
$this->load->model('post_model', 'PostModel');
6868

@@ -79,6 +79,20 @@ $posts = $this->PostModel->find()
7979
->result_array();
8080
```
8181

82+
### CRUD
83+
```php
84+
$result = $this->PostModel->insert(['title' => 'Codeigniter Model']);
85+
// Find out the record which just be inserted
86+
$post = $this->PostModel->find()
87+
->order_by('id', 'DESC')
88+
->get()
89+
->row_array();
90+
// Update the record
91+
$result = $this->PostModel->update(['title' => 'CI3 Model'], $post['id']);
92+
// Delete the record
93+
$result = $this->PostModel->delete($post['id']);
94+
```
95+
8296
---
8397

8498
INSTALLATION
@@ -101,24 +115,24 @@ $config['composer_autoload'] = TRUE;
101115
CONFIGURATION
102116
-------------
103117

104-
After installation, `\BaseModel` class is ready to use. Simply, you could create a model to extend the `BaseModel` directly:
118+
After installation, `yidas\Model` class is ready to use. Simply, you could create a model to extend the `yidas\Model` directly:
105119

106120
```php
107-
class Post_model extends BaseModel {}
121+
class Post_model extends yidas\Model {}
108122
```
109123

110124
After that, this model is ready to use for example: `$this->PostModel->findOne(123);`
111125

112-
However, the schema of tables such as primary key in your applicaiton may not same as default, and it's annoying to defind repeated schema for each model. We recommend you to make `My_model` to extend `BaseModel` instead.
126+
However, the schema of tables such as primary key in your applicaiton may not same as default, and it's annoying to defind repeated schema for each model. We recommend you to make `My_model` to extend `yidas\Model` instead.
113127

114-
### Use My_model to Extend BaseModel for every Models
128+
### Use My_model to Extend Base Model for every Models
115129

116-
You could use `My_model` to extend `BaseModel`, then make each model to extend `My_model` in Codeigniter application.
130+
You could use `My_model` to extend `yidas\Model`, then make each model to extend `My_model` in Codeigniter application.
117131

118-
*1. Create `My_model` extended `BaseModel` with configuration for fitting your common table schema:*
132+
*1. Create `My_model` extended `yidas\Model` with configuration for fitting your common table schema:*
119133

120134
```php
121-
class My_model extends BaseModel
135+
class My_model extends yidas\Model
122136
{
123137
protected $primaryKey = 'sn';
124138
const CREATED_AT = 'created_time';
@@ -151,14 +165,14 @@ $post = $this->PostModel->findOne(123);
151165
Defining Models
152166
---------------
153167

154-
To get started, let's create an model extends `BaseModel` or through `My_model`, then define each model suitably.
168+
To get started, let's create an model extends `yidas\Model` or through `My_model`, then define each model suitably.
155169

156170
### Table Names
157171

158-
By convention, the "snake case" with lowercase excluded `_model` postfix of the class name will be used as the table name unless another name is explicitly specified. So, in this case, BaseModel will assume the `Post_model` model stores records in the `post` table. You may specify a custom table by defining a table property on your model:
172+
By convention, the "snake case" with lowercase excluded `_model` postfix of the class name will be used as the table name unless another name is explicitly specified. So, in this case, Model will assume the `Post_model` model stores records in the `post` table. You may specify a custom table by defining a table property on your model:
159173

160174
```php
161-
// class My_model extends BaseModel
175+
// class My_model extends yidas\Model
162176
class Post_model extends My_model
163177
{
164178
protected $table = "post_table";
@@ -182,18 +196,18 @@ In our pattern, The naming between model class and table is the same, with suppo
182196
You may define a protected `$primaryKey` property to override this convention:
183197

184198
```php
185-
class My_model extends BaseModel
199+
class My_model extends yidas\Model
186200
{
187201
   protected $primaryKey = "sn";
188202
}
189203
```
190204

191205
### Timestamps
192206

193-
By default, BaseModel expects `created_at` and `updated_at` columns to exist on your tables. If you do not wish to have these columns automatically managed by BaseModel, set the `$timestamps` property on your model as `false`:
207+
By default, Model expects `created_at` and `updated_at` columns to exist on your tables. If you do not wish to have these columns automatically managed by base Model, set the `$timestamps` property on your model as `false`:
194208

195209
```php
196-
class My_model extends BaseModel
210+
class My_model extends yidas\Model
197211
{
198212
protected $timestamps = false;
199213
}
@@ -202,7 +216,7 @@ class My_model extends BaseModel
202216
If you need to customize the format of your timestamps, set the `$dateFormat` property on your model. This property determines how date attributes are stored in the database:
203217

204218
```php
205-
class My_model extends BaseModel
219+
class My_model extends yidas\Model
206220
{
207221
/**
208222
* Date format for timestamps.
@@ -216,7 +230,7 @@ class My_model extends BaseModel
216230
If you need to customize the names of the columns used to store the timestamps, you may set the `CREATED_AT` and `UPDATED_AT` constants in your model:
217231

218232
```php
219-
class My_model extends BaseModel
233+
class My_model extends yidas\Model
220234
{
221235
const CREATED_AT = 'created_time';
222236
const UPDATED_AT = 'updated_time';
@@ -226,7 +240,7 @@ class My_model extends BaseModel
226240
Also, you could customized turn timestamps behavior off for specified column by assigning as empty:
227241

228242
```php
229-
class My_model extends BaseModel
243+
class My_model extends yidas\Model
230244
{
231245
const CREATED_AT = 'created_time';
232246
const UPDATED_AT = NULL;
@@ -362,7 +376,7 @@ In addition to actually removing records from your database, This Model can also
362376
You could enable SOFT DELETED feature by giving field name to `SOFT_DELETED`:
363377

364378
```php
365-
class My_model extends BaseModel
379+
class My_model extends yidas\Model
366380
{
367381
const SOFT_DELETED = 'is_deleted';
368382
}
@@ -371,7 +385,7 @@ class My_model extends BaseModel
371385
While `SOFT_DELETED` is enabled, you could set `$softDeletedFalseValue` and `$softDeletedTrueValue` for fitting table schema. Futher, you may set `DELETED_AT` with column name for Timestapes feature, or disabled by setting to `NULL` by default:
372386

373387
```php
374-
class My_model extends BaseModel
388+
class My_model extends yidas\Model
375389
{
376390
const SOFT_DELETED = 'is_deleted';
377391

@@ -388,7 +402,7 @@ class My_model extends BaseModel
388402
If you need to disabled SOFT DELETED feature for specified model, you may set `SOFT_DELETED` to `false`, which would disable any SOFT DELETED functions including `DELETED_AT` feature:
389403

390404
```php
391-
// class My_model extends BaseModel
405+
// class My_model extends yidas\Model
392406
class Log_model extends My_model
393407
{
394408
const SOFT_DELETED = false;
@@ -449,7 +463,7 @@ Query scopes allow you to add constraints to all queries for a given model. Writ
449463
You could override `_globalScopes` method to define your constraints:
450464

451465
```php
452-
class My_model extends BaseModel
466+
class My_model extends yidas\Model
453467
{
454468
protected $userAttribute = 'uid';
455469

@@ -499,7 +513,7 @@ Sometimes you may wish to use one database connection for `SELECT` statements, a
499513

500514
### Configuration
501515

502-
Read & Write Connections could be set in the model which extends `BaseModel`, you could defind the read & write databases in extended `My_model` for every models.
516+
Read & Write Connections could be set in the model which extends `yidas\Model`, you could defind the read & write databases in extended `My_model` for every models.
503517

504518
There are three types to set read & write databases:
505519

@@ -509,7 +523,7 @@ There are three types to set read & write databases:
509523
You could set the database key refered from `\application\config\database.php` into model attributes of `database` & `databaseRead`, the setting connections would be created automatically:
510524

511525
```php
512-
class My_model extends BaseModel
526+
class My_model extends yidas\Model
513527
{
514528
protected $database = 'default';
515529

@@ -524,7 +538,7 @@ class My_model extends BaseModel
524538
If you already have prepared CI DB connections, you could assign to attributes directly in construct section before parent's constrcut:
525539

526540
```php
527-
class My_model extends BaseModel
541+
class My_model extends yidas\Model
528542
{
529543
function __construct()
530544
{
@@ -542,7 +556,7 @@ class My_model extends BaseModel
542556
This way is used for the specified model related to the one time connected database in a request cycle, which would create a new connection per each model:
543557

544558
```php
545-
class My_model extends BaseModel
559+
class My_model extends yidas\Model
546560
{
547561
protected $databaseRead = [
548562
'dsn' => '',
@@ -567,7 +581,7 @@ $db['slave']['hostname'] = $slaveHosts[mt_rand(0, count($slaveHosts) - 1)];
567581
After that, you could use database key `slave` to load or assign it to attribute:
568582

569583
```php
570-
class My_model extends BaseModel
584+
class My_model extends yidas\Model
571585
{
572586
protected $databaseRead = 'slave';
573587
}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
},
1212
"minimum-stability": "stable",
1313
"autoload": {
14-
"classmap": ["src/"]
14+
"psr-4": {
15+
"yidas\\": "src/"
16+
}
1517
}
1618
}

example/My_model.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
/**
44
* My_model
55
*
6-
* Based on BaseModel, My_model is customized for your web application with features, such as the
7-
* verification of user ID and company ID for multiple user layers.
6+
* Based on yidas\Model, My_model is customized for your web application with schema such as
7+
* primary key and column names for behavior setting. Futher, all of your model may need access
8+
* features, such as the verification of user ID and company ID for multiple user layers.
9+
*
810
* This example My_model assumes that a user is belong to a company, so each data row is belong to
911
* a user with that company. The Model basic funcitons overrided BaseModel with user and company
1012
* verification to implement the protection.
1113
*
1214
* @author Nick Tsai <myintaer@gmail.com>
15+
* @version 2.0.0
16+
* @see https://github.com/yidas/codeigniter-model/tree/master/example
17+
* @since \yidas\Mdoel 2.0.0
1318
* @see https://github.com/yidas/codeigniter-model
14-
* @since BaseMdoel 0.15.0
1519
*/
16-
class My_model extends BaseModel
20+
class My_model extends yidas\Model
1721
{
1822
/* Configuration by Inheriting */
1923

@@ -35,7 +39,7 @@ class My_model extends BaseModel
3539
protected $dateFormat = 'unixtime';
3640

3741
// Record status for checking is deleted or not
38-
const RECORD_DELETED = 'is_deleted';
42+
const SOFT_DELETED = 'is_deleted';
3943

4044
// 0: actived, 1: deleted
4145
protected $recordDeletedFalseValue = '1';
@@ -111,7 +115,7 @@ protected function _globalScopes()
111115

112116
$this->getBuilder()->where(
113117
$this->_field($this->companyAttribute),
114-
$this->$companyID
118+
$this->companyID
115119
);
116120
}
117121

@@ -122,58 +126,50 @@ protected function _globalScopes()
122126
$this->userID
123127
);
124128
}
125-
126129
return parent::_globalScopes();
127130
}
128-
129131
/**
130132
* Override _attrEventBeforeInsert()
131133
*/
132134
protected function _attrEventBeforeInsert(&$attributes)
133135
{
134136
// Auto Company
135-
if ($this->companyAttribute && !isset($attr[$this->companyAttribute])) {
137+
if ($this->companyAttribute && !isset($attributes[$this->companyAttribute])) {
136138

137-
$attributes[$this->companyAttribute] = $this->companySN;
139+
$attributes[$this->companyAttribute] = $this->companyID;
138140
}
139-
140141
// Auto User
141-
if ($this->userAttribute && !isset($attr[$this->userAttribute])) {
142+
if ($this->userAttribute && !isset($attributes[$this->userAttribute])) {
142143

143-
$attributes[$this->userAttribute] = $this->userSN;
144+
$attributes[$this->userAttribute] = $this->userID;
144145
}
145-
146146
// Auto created_by
147-
if ($this->createdUserAttribute) {
148-
$attributes[$this->createdUserAttribute] = $this->userSN;
147+
if ($this->createdUserAttribute && !isset($attributes[$this->createdUserAttribute])) {
148+
$attributes[$this->createdUserAttribute] = $this->userID;
149149
}
150150

151151
return parent::_attrEventBeforeInsert($attributes);
152152
}
153-
154153
/**
155154
* Override _attrEventBeforeUpdate()
156155
*/
157156
public function _attrEventBeforeUpdate(&$attributes)
158157
{
159158
// Auto updated_by
160-
if ($this->updatedUserAttribute) {
161-
$attributes[$this->updatedUserAttribute] = $this->userSN;
159+
if ($this->updatedUserAttribute && !isset($attributes[$this->updatedUserAttribute])) {
160+
$attributes[$this->updatedUserAttribute] = $this->userID;
162161
}
163-
164162
return parent::_attrEventBeforeUpdate($attributes);
165163
}
166-
167164
/**
168165
* Override _attrEventBeforeDelete()
169166
*/
170167
public function _attrEventBeforeDelete(&$attributes)
171168
{
172169
// Auto deleted_by
173-
if ($this->deletedUserAttribute) {
174-
$attributes[$this->deletedUserAttribute] = $this->userSN;
170+
if ($this->deletedUserAttribute && !isset($attributes[$this->deletedUserAttribute])) {
171+
$attributes[$this->deletedUserAttribute] = $this->userID;
175172
}
176-
177173
return parent::_attrEventBeforeDelete($attributes);
178174
}
179175
}

example/README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Example of My_model
22
===================
33

4-
You could make My_model extends `BaseModel` for each model in your application.
4+
The best practice to use `BaseModel` is using `My_model` to extend for every models, you could refer the document [Use My_model to Extend BaseModel for every Models](https://github.com/yidas/codeigniter-model#use-my_model-to-extend-basemodel-for-every-models) for building the structure in your Codeigniter application.
55

6-
Example of [My_model](https://github.com/yidas/codeigniter-model/blob/master/example/My_model.php):
6+
[My_model example code](https://github.com/yidas/codeigniter-model/blob/master/example/My_model.php)
77

8-
>Based on BaseModel, My_model is customized for your web application with features, such as the verification of user ID and company ID for multiple user layers.
8+
>Based on BaseModel, My_model is customized for your web application with schema such as primary key and column names for behavior setting. Futher, all of your model may need access features, such as the verification of user ID and company ID for multiple user layers.
99
1010
---
1111

@@ -14,6 +14,37 @@ Features
1414

1515
This example My_model assumes that a user is belong to a company, so each data row is belong to a user with that company. The Model basic funcitons overrided BaseModel with user and company verification to implement the protection.
1616

17+
---
18+
19+
CONFIGURATION
20+
-------------
21+
22+
```php
23+
class My_model extends BaseModel
24+
{
25+
/* Configuration by Inheriting */
26+
27+
// The regular PK Key in App
28+
protected $primaryKey = 'id';
29+
// Timestamps on
30+
protected $timestamps = true;
31+
// Soft Deleted on
32+
const SOFT_DELETED = 'is_deleted';
33+
34+
protected function _globalScopes()
35+
{
36+
// Global Scope...
37+
}
38+
39+
protected function _attrEventBeforeInsert(&$attributes)
40+
{
41+
// Insert Behavior...
42+
}
43+
44+
// Other Behaviors...
45+
}
46+
```
47+
1748

1849
Defining Models
1950
---------------

0 commit comments

Comments
 (0)