Skip to content

Commit 6363eb1

Browse files
committed
PSR-4 support with static call
1 parent 896bb82 commit 6363eb1

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

src/Model.php

Lines changed: 56 additions & 22 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.14.0
11+
* @version 2.15.0
1212
* @see https://github.com/yidas/codeigniter-model
1313
*/
1414
class Model extends \CI_Model implements \ArrayAccess
@@ -464,32 +464,34 @@ public function setAlias($alias)
464464
*/
465465
public function find($withAll=false)
466466
{
467+
$instance = new static;
468+
467469
// One time setting reset mechanism
468-
if ($this->_cleanNextFind === true) {
470+
if ($instance->_cleanNextFind === true) {
469471
// Reset alias
470-
$this->setAlias(null);
472+
$instance->setAlias(null);
471473
} else {
472474
// Turn on clean for next find
473-
$this->_cleanNextFind = true;
475+
$instance->_cleanNextFind = true;
474476
}
475477

476478
// Alias option for FROM
477-
$sqlFrom = ($this->alias) ? "{$this->table} AS {$this->alias}" : $this->table;
479+
$sqlFrom = ($instance->alias) ? "{$instance->table} AS {$instance->alias}" : $instance->table;
478480

479-
$this->_dbr->from($sqlFrom);
481+
$instance->_dbr->from($sqlFrom);
480482

481483
// WithAll helper
482484
if ($withAll===true) {
483-
$this->withAll();
485+
$instance->withAll();
484486
}
485487

486488
// Scope condition
487-
$this->_addGlobalScopeCondition();
489+
$instance->_addGlobalScopeCondition();
488490

489491
// Soft Deleted condition
490-
$this->_addSoftDeletedCondition();
492+
$instance->_addSoftDeletedCondition();
491493

492-
return $this->_dbr;
494+
return $instance->_dbr;
493495
}
494496

495497
/**
@@ -514,9 +516,11 @@ public function forceFind()
514516
* $this->Model->find()->where('id', 123);
515517
* $this->Model->findOne();
516518
*/
517-
public function findOne($condition=[])
519+
public static function findOne($condition=[])
518520
{
519-
$record = $this->_findByCondition($condition)
521+
$instance = new static;
522+
523+
$record = $instance->_findByCondition($condition)
520524
->limit(1)
521525
->get()->row_array();
522526

@@ -525,7 +529,7 @@ public function findOne($condition=[])
525529
return $record;
526530
}
527531

528-
return $this->createActiveRecord($record, $record[$this->primaryKey]);
532+
return $instance->createActiveRecord($record, $record[$instance->primaryKey]);
529533
}
530534

531535
/**
@@ -540,9 +544,11 @@ public function findOne($condition=[])
540544
* $this->Model->find()->where_in('id', [3,21,135]);
541545
* $this->Model->findAll();
542546
*/
543-
public function findAll($condition=[])
547+
public static function findAll($condition=[])
544548
{
545-
$records = $this->_findByCondition($condition)
549+
$instance = new static;
550+
551+
$records = $instance->_findByCondition($condition)
546552
->get()->result_array();
547553

548554
// Record check
@@ -554,11 +560,11 @@ public function findAll($condition=[])
554560
// Each ActiveRecord
555561
foreach ((array)$records as $key => $record) {
556562
// Check primary key setting
557-
if (!isset($record[$this->primaryKey])) {
563+
if (!isset($record[$instance->primaryKey])) {
558564
throw new Exception("Model's primary key not set", 500);
559565
}
560566
// Create an ActiveRecord into collect
561-
$set[] = $this->createActiveRecord($record, $record[$this->primaryKey]);
567+
$set[] = $instance->createActiveRecord($record, $record[$instance->primaryKey]);
562568
}
563569

564570
return $set;
@@ -1118,20 +1124,33 @@ public function hasOne($modelName, $foreignKey=null, $localKey=null)
11181124
*/
11191125
protected function _relationship($modelName, $relationship, $foreignKey=null, $localKey=null)
11201126
{
1121-
$this->load->model($modelName);
1127+
/**
1128+
* PSR-4 support check
1129+
*
1130+
* @see https://github.com/yidas/codeigniter-psr4-autoload
1131+
*/
1132+
if (strpos($modelName, "\\") !== false ) {
1133+
1134+
$model = new $modelName;
1135+
1136+
} else {
1137+
// Original CodeIgniter 3 model loader
1138+
$this->load->model($modelName);
1139+
$model = $this->$modelName;
1140+
}
11221141

11231142
$libClass = __CLASS__;
11241143

11251144
// Check if is using same library
1126-
if (!is_subclass_of($this->$modelName, $libClass)) {
1145+
if (!is_subclass_of($model, $libClass)) {
11271146
throw new Exception("Model `{$modelName}` does not extend {$libClass}", 500);
11281147
}
11291148

11301149
// Keys
11311150
$foreignKey = ($foreignKey) ? $foreignKey : $this->primaryKey;
11321151
$localKey = ($localKey) ? $localKey : $this->primaryKey;
11331152

1134-
$query = $this->$modelName->find()
1153+
$query = $model->find()
11351154
->where($foreignKey, $this->$localKey);
11361155

11371156
// Inject Model name into query builder for ORM relationships
@@ -1491,16 +1510,31 @@ public function __get($name)
14911510
throw new Exception("ORM relationships error", 500);
14921511
}
14931512

1513+
/**
1514+
* PSR-4 support check
1515+
*
1516+
* @see https://github.com/yidas/codeigniter-psr4-autoload
1517+
*/
1518+
if (strpos($modelName, "\\") !== false ) {
1519+
1520+
$model = new $modelName;
1521+
1522+
} else {
1523+
// Original CodeIgniter 3 model loader
1524+
$this->load->model($modelName);
1525+
$model = $this->$modelName;
1526+
}
1527+
14941528
// Check return type
14951529
if ($relationship == 'hasOne') {
14961530

14971531
// Keep same query builder from hasOne()
1498-
return $this->$modelName->findOne(null);
1532+
return $model->findOne(null);
14991533

15001534
} else {
15011535

15021536
// Keep same query builder from hasMany()
1503-
return $this->$modelName->findAll(null);
1537+
return $model->findAll(null);
15041538
}
15051539

15061540
}

0 commit comments

Comments
 (0)