8
8
* Base Model
9
9
*
10
10
* @author Nick Tsai <myintaer@gmail.com>
11
- * @version 2.15 .0
11
+ * @version 2.16 .0
12
12
* @see https://github.com/yidas/codeigniter-model
13
13
*/
14
14
class Model extends \CI_Model implements \ArrayAccess
@@ -191,13 +191,13 @@ function __construct()
191
191
$ this ->_db = self ::$ _dbCaches [$ this ->database ];
192
192
} else {
193
193
// CI Database Configuration
194
- $ this ->_db = $ this ->load ->database ($ this ->database , true );
194
+ $ this ->_db = get_instance () ->load ->database ($ this ->database , true );
195
195
self ::$ _dbCaches [$ this ->database ] = $ this ->_db ;
196
196
}
197
197
}
198
198
else {
199
199
// Config array for each Model
200
- $ this ->_db = $ this ->load ->database ($ this ->database , true );
200
+ $ this ->_db = get_instance () ->load ->database ($ this ->database , true );
201
201
}
202
202
} else {
203
203
// CI Default DB Connection
@@ -215,13 +215,13 @@ function __construct()
215
215
$ this ->_dbr = self ::$ _dbrCaches [$ this ->databaseRead ];
216
216
} else {
217
217
// CI Database Configuration
218
- $ this ->_dbr = $ this ->load ->database ($ this ->databaseRead , true );
218
+ $ this ->_dbr = get_instance () ->load ->database ($ this ->databaseRead , true );
219
219
self ::$ _dbrCaches [$ this ->databaseRead ] = $ this ->_dbr ;
220
220
}
221
221
}
222
222
else {
223
223
// Config array for each Model
224
- $ this ->_dbr = $ this ->load ->database ($ this ->databaseRead , true );
224
+ $ this ->_dbr = get_instance () ->load ->database ($ this ->databaseRead , true );
225
225
}
226
226
} else {
227
227
// CI Default DB Connection
@@ -343,17 +343,19 @@ public function validate($attributes=[], $returnData=false)
343
343
return ($ returnData ) ? $ data : true ;
344
344
345
345
// Load CodeIgniter form_validation library for yidas/model namespace, which has no effect on common one
346
- $ this ->load ->library ('form_validation ' , null , 'yidas_model_form_validation ' );
347
- $ this ->yidas_model_form_validation ->reset_validation ();
348
- $ this ->yidas_model_form_validation ->set_data ($ data );
349
- $ this ->yidas_model_form_validation ->set_rules ($ rules );
346
+ get_instance ()->load ->library ('form_validation ' , null , 'yidas_model_form_validation ' );
347
+ // Get CodeIgniter validator
348
+ $ validator = get_instance ()->yidas_model_form_validation ;
349
+ $ validator ->reset_validation ();
350
+ $ validator ->set_data ($ data );
351
+ $ validator ->set_rules ($ rules );
350
352
// Run Validate
351
- $ result = $ this -> yidas_model_form_validation ->run ();
353
+ $ result = $ validator ->run ();
352
354
353
355
// Result handle
354
356
if ($ result ===false ) {
355
357
356
- $ this ->_errors = $ this -> yidas_model_form_validation ->error_array ();
358
+ $ this ->_errors = $ validator ->error_array ();
357
359
return false ;
358
360
359
361
} else {
@@ -1135,7 +1137,7 @@ protected function _relationship($modelName, $relationship, $foreignKey=null, $l
1135
1137
1136
1138
} else {
1137
1139
// Original CodeIgniter 3 model loader
1138
- $ this ->load ->model ($ modelName );
1140
+ get_instance () ->load ->model ($ modelName );
1139
1141
$ model = $ this ->$ modelName ;
1140
1142
}
1141
1143
@@ -1458,10 +1460,10 @@ private function _getDefaultDB()
1458
1460
}
1459
1461
1460
1462
if (!isset ($ this ->db )) {
1461
- $ this ->load ->database ();
1463
+ get_instance () ->load ->database ();
1462
1464
}
1463
1465
// No need to set as reference because $this->db is refered to &DB already.
1464
- return $ this ->db ;
1466
+ return get_instance () ->db ;
1465
1467
}
1466
1468
1467
1469
/**
@@ -1482,12 +1484,6 @@ public function __set($name, $value)
1482
1484
*/
1483
1485
public function __get ($ name )
1484
1486
{
1485
- // CI parent::__get() check
1486
- if (property_exists (get_instance (), $ name )) {
1487
-
1488
- return parent ::__get ($ name );
1489
- }
1490
-
1491
1487
// ORM property check
1492
1488
if (array_key_exists ($ name , $ this ->_writeProperties ) ) {
1493
1489
@@ -1521,7 +1517,7 @@ public function __get($name)
1521
1517
1522
1518
} else {
1523
1519
// Original CodeIgniter 3 model loader
1524
- $ this ->load ->model ($ modelName );
1520
+ get_instance () ->load ->model ($ modelName );
1525
1521
$ model = $ this ->$ modelName ;
1526
1522
}
1527
1523
@@ -1567,6 +1563,12 @@ public function __get($name)
1567
1563
return $ this ->_readProperties [$ name ];
1568
1564
}
1569
1565
1566
+ // CI parent::__get() check
1567
+ if (property_exists (get_instance (), $ name )) {
1568
+
1569
+ return parent ::__get ($ name );
1570
+ }
1571
+
1570
1572
// Exception
1571
1573
throw new \Exception ("Property ` {$ name }` does not exist " , 500 );
1572
1574
}
0 commit comments