Skip to content

Commit 7c71c09

Browse files
committed
Release version 1.1.1
1 parent 16d752a commit 7c71c09

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ To get started, let's create an model extends `BaseModel` or through `My_model`,
155155

156156
### Table Names
157157

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 `Posts_model` model stores records in the `posts` table. You may specify a custom table by defining a table property on your model:
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:
159159

160160
```php
161-
class Post_model extends BaseModel
161+
// class My_model extends BaseModel
162+
class Post_model extends My_model
162163
{
163164
protected $table = "post_table";
164165
}
@@ -178,10 +179,10 @@ In our pattern, The naming between model class and table is the same, with suppo
178179

179180
### Primary Keys
180181

181-
You may define a protected $primaryKey property to override this convention.
182+
You may define a protected `$primaryKey` property to override this convention:
182183

183184
```php
184-
class Post_model extends BaseModel
185+
class My_model extends BaseModel
185186
{
186187
   protected $primaryKey = "sn";
187188
}
@@ -387,7 +388,8 @@ class My_model extends BaseModel
387388
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:
388389

389390
```php
390-
class My_model extends BaseModel
391+
// class My_model extends BaseModel
392+
class Log_model extends My_model
391393
{
392394
const SOFT_DELETED = false;
393395
}
@@ -504,7 +506,7 @@ There are three types to set read & write databases:
504506

505507
#### Codeigniter Database Key
506508

507-
You could set the database key refering from `\application\config\database.php` to `database` & `databaseRead` attribute, the setting connections would be created automatically:
509+
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:
508510

509511
```php
510512
class My_model extends BaseModel
@@ -562,5 +564,12 @@ $slaveHosts = ['192.168.1.2', '192.168.1.3'];
562564
$db['slave']['hostname'] = $slaveHosts[mt_rand(0, count($slaveHosts) - 1)];
563565
```
564566

565-
After that, you could use database key `slave` to load or assign it to attribute likes: `protected $databaseRead = 'slave';`
567+
After that, you could use database key `slave` to load or assign it to attribute:
568+
569+
```php
570+
class My_model extends BaseModel
571+
{
572+
protected $databaseRead = 'slave';
573+
}
574+
```
566575

src/BaseModel.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Base Model
55
*
66
* @author Nick Tsai <myintaer@gmail.com>
7-
* @version 1.1.0
7+
* @version 1.1.1
88
* @see https://github.com/yidas/codeigniter-model
99
*/
1010
class BaseModel extends CI_Model
@@ -155,7 +155,7 @@ function __construct()
155155
}
156156
} else {
157157
// CI Default DB Connection
158-
$this->_db = $this->db; // No need to set as reference because $this->db is refered to &DB already.
158+
$this->_db = $this->_getDefaultDB();
159159
}
160160
// Slave
161161
if ($this->databaseRead) {
@@ -179,7 +179,7 @@ function __construct()
179179
}
180180
} else {
181181
// CI Default DB Connection
182-
$this->_dbr = $this->db; // No need to set as reference because $this->db is refered to &DB already.
182+
$this->_dbr = $this->_getDefaultDB();
183183
}
184184

185185
/* Table Name Guessing */
@@ -778,4 +778,18 @@ protected function _field($columnName)
778778
{
779779
return "`{$this->table}`.`{$columnName}`";
780780
}
781+
782+
/**
783+
* Get & load $this->db in CI application
784+
*
785+
* @return object CI $this->db
786+
*/
787+
private function _getDefaultDB()
788+
{
789+
if (!isset($this->db)) {
790+
$this->load->database();
791+
}
792+
// No need to set as reference because $this->db is refered to &DB already.
793+
return $this->db;
794+
}
781795
}

0 commit comments

Comments
 (0)