Skip to content

Commit 2ae678b

Browse files
committed
High priority for ORM porperties
1 parent fe1cec3 commit 2ae678b

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/Model.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Base Model
99
*
1010
* @author Nick Tsai <myintaer@gmail.com>
11-
* @version 2.15.0
11+
* @version 2.16.0
1212
* @see https://github.com/yidas/codeigniter-model
1313
*/
1414
class Model extends \CI_Model implements \ArrayAccess
@@ -191,13 +191,13 @@ function __construct()
191191
$this->_db = self::$_dbCaches[$this->database];
192192
} else {
193193
// CI Database Configuration
194-
$this->_db = $this->load->database($this->database, true);
194+
$this->_db = get_instance()->load->database($this->database, true);
195195
self::$_dbCaches[$this->database] = $this->_db;
196196
}
197197
}
198198
else {
199199
// Config array for each Model
200-
$this->_db = $this->load->database($this->database, true);
200+
$this->_db = get_instance()->load->database($this->database, true);
201201
}
202202
} else {
203203
// CI Default DB Connection
@@ -215,13 +215,13 @@ function __construct()
215215
$this->_dbr = self::$_dbrCaches[$this->databaseRead];
216216
} else {
217217
// CI Database Configuration
218-
$this->_dbr = $this->load->database($this->databaseRead, true);
218+
$this->_dbr = get_instance()->load->database($this->databaseRead, true);
219219
self::$_dbrCaches[$this->databaseRead] = $this->_dbr;
220220
}
221221
}
222222
else {
223223
// Config array for each Model
224-
$this->_dbr = $this->load->database($this->databaseRead, true);
224+
$this->_dbr = get_instance()->load->database($this->databaseRead, true);
225225
}
226226
} else {
227227
// CI Default DB Connection
@@ -343,17 +343,19 @@ public function validate($attributes=[], $returnData=false)
343343
return ($returnData) ? $data : true;
344344

345345
// 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);
350352
// Run Validate
351-
$result = $this->yidas_model_form_validation->run();
353+
$result = $validator->run();
352354

353355
// Result handle
354356
if ($result===false) {
355357

356-
$this->_errors = $this->yidas_model_form_validation->error_array();
358+
$this->_errors = $validator->error_array();
357359
return false;
358360

359361
} else {
@@ -1135,7 +1137,7 @@ protected function _relationship($modelName, $relationship, $foreignKey=null, $l
11351137

11361138
} else {
11371139
// Original CodeIgniter 3 model loader
1138-
$this->load->model($modelName);
1140+
get_instance()->load->model($modelName);
11391141
$model = $this->$modelName;
11401142
}
11411143

@@ -1458,10 +1460,10 @@ private function _getDefaultDB()
14581460
}
14591461

14601462
if (!isset($this->db)) {
1461-
$this->load->database();
1463+
get_instance()->load->database();
14621464
}
14631465
// No need to set as reference because $this->db is refered to &DB already.
1464-
return $this->db;
1466+
return get_instance()->db;
14651467
}
14661468

14671469
/**
@@ -1482,12 +1484,6 @@ public function __set($name, $value)
14821484
*/
14831485
public function __get($name)
14841486
{
1485-
// CI parent::__get() check
1486-
if (property_exists(get_instance(), $name)) {
1487-
1488-
return parent::__get($name);
1489-
}
1490-
14911487
// ORM property check
14921488
if (array_key_exists($name, $this->_writeProperties) ) {
14931489

@@ -1521,7 +1517,7 @@ public function __get($name)
15211517

15221518
} else {
15231519
// Original CodeIgniter 3 model loader
1524-
$this->load->model($modelName);
1520+
get_instance()->load->model($modelName);
15251521
$model = $this->$modelName;
15261522
}
15271523

@@ -1567,6 +1563,12 @@ public function __get($name)
15671563
return $this->_readProperties[$name];
15681564
}
15691565

1566+
// CI parent::__get() check
1567+
if (property_exists(get_instance(), $name)) {
1568+
1569+
return parent::__get($name);
1570+
}
1571+
15701572
// Exception
15711573
throw new \Exception("Property `{$name}` does not exist", 500);
15721574
}

0 commit comments

Comments
 (0)