You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
105
119
106
120
```php
107
-
class Post_model extends BaseModel {}
121
+
class Post_model extends yidas\Model {}
108
122
```
109
123
110
124
After that, this model is ready to use for example: `$this->PostModel->findOne(123);`
111
125
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.
113
127
114
-
### Use My_model to Extend BaseModel for every Models
128
+
### Use My_model to Extend Base Model for every Models
115
129
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.
117
131
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:*
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.
155
169
156
170
### Table Names
157
171
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:
159
173
160
174
```php
161
-
// class My_model extends BaseModel
175
+
// class My_model extends yidas\Model
162
176
class Post_model extends My_model
163
177
{
164
178
protected $table = "post_table";
@@ -182,18 +196,18 @@ In our pattern, The naming between model class and table is the same, with suppo
182
196
You may define a protected `$primaryKey` property to override this convention:
183
197
184
198
```php
185
-
class My_model extends BaseModel
199
+
class My_model extends yidas\Model
186
200
{
187
201
protected $primaryKey = "sn";
188
202
}
189
203
```
190
204
191
205
### Timestamps
192
206
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`:
194
208
195
209
```php
196
-
class My_model extends BaseModel
210
+
class My_model extends yidas\Model
197
211
{
198
212
protected $timestamps = false;
199
213
}
@@ -202,7 +216,7 @@ class My_model extends BaseModel
202
216
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:
203
217
204
218
```php
205
-
class My_model extends BaseModel
219
+
class My_model extends yidas\Model
206
220
{
207
221
/**
208
222
* Date format for timestamps.
@@ -216,7 +230,7 @@ class My_model extends BaseModel
216
230
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:
217
231
218
232
```php
219
-
class My_model extends BaseModel
233
+
class My_model extends yidas\Model
220
234
{
221
235
const CREATED_AT = 'created_time';
222
236
const UPDATED_AT = 'updated_time';
@@ -226,7 +240,7 @@ class My_model extends BaseModel
226
240
Also, you could customized turn timestamps behavior off for specified column by assigning as empty:
227
241
228
242
```php
229
-
class My_model extends BaseModel
243
+
class My_model extends yidas\Model
230
244
{
231
245
const CREATED_AT = 'created_time';
232
246
const UPDATED_AT = NULL;
@@ -362,7 +376,7 @@ In addition to actually removing records from your database, This Model can also
362
376
You could enable SOFT DELETED feature by giving field name to `SOFT_DELETED`:
363
377
364
378
```php
365
-
class My_model extends BaseModel
379
+
class My_model extends yidas\Model
366
380
{
367
381
const SOFT_DELETED = 'is_deleted';
368
382
}
@@ -371,7 +385,7 @@ class My_model extends BaseModel
371
385
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:
372
386
373
387
```php
374
-
class My_model extends BaseModel
388
+
class My_model extends yidas\Model
375
389
{
376
390
const SOFT_DELETED = 'is_deleted';
377
391
@@ -388,7 +402,7 @@ class My_model extends BaseModel
388
402
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:
389
403
390
404
```php
391
-
// class My_model extends BaseModel
405
+
// class My_model extends yidas\Model
392
406
class Log_model extends My_model
393
407
{
394
408
const SOFT_DELETED = false;
@@ -449,7 +463,7 @@ Query scopes allow you to add constraints to all queries for a given model. Writ
449
463
You could override `_globalScopes` method to define your constraints:
450
464
451
465
```php
452
-
class My_model extends BaseModel
466
+
class My_model extends yidas\Model
453
467
{
454
468
protected $userAttribute = 'uid';
455
469
@@ -499,7 +513,7 @@ Sometimes you may wish to use one database connection for `SELECT` statements, a
499
513
500
514
### Configuration
501
515
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.
503
517
504
518
There are three types to set read & write databases:
505
519
@@ -509,7 +523,7 @@ There are three types to set read & write databases:
509
523
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:
510
524
511
525
```php
512
-
class My_model extends BaseModel
526
+
class My_model extends yidas\Model
513
527
{
514
528
protected $database = 'default';
515
529
@@ -524,7 +538,7 @@ class My_model extends BaseModel
524
538
If you already have prepared CI DB connections, you could assign to attributes directly in construct section before parent's constrcut:
525
539
526
540
```php
527
-
class My_model extends BaseModel
541
+
class My_model extends yidas\Model
528
542
{
529
543
function __construct()
530
544
{
@@ -542,7 +556,7 @@ class My_model extends BaseModel
542
556
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:
Copy file name to clipboardExpand all lines: example/README.md
+34-3Lines changed: 34 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
Example of My_model
2
2
===================
3
3
4
-
You could make My_model extends`BaseModel` for each modelin 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.
5
5
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)
7
7
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.
9
9
10
10
---
11
11
@@ -14,6 +14,37 @@ Features
14
14
15
15
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.
16
16
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)
0 commit comments