8
8
* Base Model
9
9
*
10
10
* @author Nick Tsai <myintaer@gmail.com>
11
- * @version 2.14 .0
11
+ * @version 2.15 .0
12
12
* @see https://github.com/yidas/codeigniter-model
13
13
*/
14
14
class Model extends \CI_Model implements \ArrayAccess
@@ -464,32 +464,34 @@ public function setAlias($alias)
464
464
*/
465
465
public function find ($ withAll =false )
466
466
{
467
+ $ instance = new static ;
468
+
467
469
// One time setting reset mechanism
468
- if ($ this ->_cleanNextFind === true ) {
470
+ if ($ instance ->_cleanNextFind === true ) {
469
471
// Reset alias
470
- $ this ->setAlias (null );
472
+ $ instance ->setAlias (null );
471
473
} else {
472
474
// Turn on clean for next find
473
- $ this ->_cleanNextFind = true ;
475
+ $ instance ->_cleanNextFind = true ;
474
476
}
475
477
476
478
// 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 ;
478
480
479
- $ this ->_dbr ->from ($ sqlFrom );
481
+ $ instance ->_dbr ->from ($ sqlFrom );
480
482
481
483
// WithAll helper
482
484
if ($ withAll ===true ) {
483
- $ this ->withAll ();
485
+ $ instance ->withAll ();
484
486
}
485
487
486
488
// Scope condition
487
- $ this ->_addGlobalScopeCondition ();
489
+ $ instance ->_addGlobalScopeCondition ();
488
490
489
491
// Soft Deleted condition
490
- $ this ->_addSoftDeletedCondition ();
492
+ $ instance ->_addSoftDeletedCondition ();
491
493
492
- return $ this ->_dbr ;
494
+ return $ instance ->_dbr ;
493
495
}
494
496
495
497
/**
@@ -514,9 +516,11 @@ public function forceFind()
514
516
* $this->Model->find()->where('id', 123);
515
517
* $this->Model->findOne();
516
518
*/
517
- public function findOne ($ condition =[])
519
+ public static function findOne ($ condition =[])
518
520
{
519
- $ record = $ this ->_findByCondition ($ condition )
521
+ $ instance = new static ;
522
+
523
+ $ record = $ instance ->_findByCondition ($ condition )
520
524
->limit (1 )
521
525
->get ()->row_array ();
522
526
@@ -525,7 +529,7 @@ public function findOne($condition=[])
525
529
return $ record ;
526
530
}
527
531
528
- return $ this ->createActiveRecord ($ record , $ record [$ this ->primaryKey ]);
532
+ return $ instance ->createActiveRecord ($ record , $ record [$ instance ->primaryKey ]);
529
533
}
530
534
531
535
/**
@@ -540,9 +544,11 @@ public function findOne($condition=[])
540
544
* $this->Model->find()->where_in('id', [3,21,135]);
541
545
* $this->Model->findAll();
542
546
*/
543
- public function findAll ($ condition =[])
547
+ public static function findAll ($ condition =[])
544
548
{
545
- $ records = $ this ->_findByCondition ($ condition )
549
+ $ instance = new static ;
550
+
551
+ $ records = $ instance ->_findByCondition ($ condition )
546
552
->get ()->result_array ();
547
553
548
554
// Record check
@@ -554,11 +560,11 @@ public function findAll($condition=[])
554
560
// Each ActiveRecord
555
561
foreach ((array )$ records as $ key => $ record ) {
556
562
// Check primary key setting
557
- if (!isset ($ record [$ this ->primaryKey ])) {
563
+ if (!isset ($ record [$ instance ->primaryKey ])) {
558
564
throw new Exception ("Model's primary key not set " , 500 );
559
565
}
560
566
// Create an ActiveRecord into collect
561
- $ set [] = $ this ->createActiveRecord ($ record , $ record [$ this ->primaryKey ]);
567
+ $ set [] = $ instance ->createActiveRecord ($ record , $ record [$ instance ->primaryKey ]);
562
568
}
563
569
564
570
return $ set ;
@@ -1118,20 +1124,33 @@ public function hasOne($modelName, $foreignKey=null, $localKey=null)
1118
1124
*/
1119
1125
protected function _relationship ($ modelName , $ relationship , $ foreignKey =null , $ localKey =null )
1120
1126
{
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
+ }
1122
1141
1123
1142
$ libClass = __CLASS__ ;
1124
1143
1125
1144
// Check if is using same library
1126
- if (!is_subclass_of ($ this -> $ modelName , $ libClass )) {
1145
+ if (!is_subclass_of ($ model , $ libClass )) {
1127
1146
throw new Exception ("Model ` {$ modelName }` does not extend {$ libClass }" , 500 );
1128
1147
}
1129
1148
1130
1149
// Keys
1131
1150
$ foreignKey = ($ foreignKey ) ? $ foreignKey : $ this ->primaryKey ;
1132
1151
$ localKey = ($ localKey ) ? $ localKey : $ this ->primaryKey ;
1133
1152
1134
- $ query = $ this -> $ modelName ->find ()
1153
+ $ query = $ model ->find ()
1135
1154
->where ($ foreignKey , $ this ->$ localKey );
1136
1155
1137
1156
// Inject Model name into query builder for ORM relationships
@@ -1491,16 +1510,31 @@ public function __get($name)
1491
1510
throw new Exception ("ORM relationships error " , 500 );
1492
1511
}
1493
1512
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
+
1494
1528
// Check return type
1495
1529
if ($ relationship == 'hasOne ' ) {
1496
1530
1497
1531
// Keep same query builder from hasOne()
1498
- return $ this -> $ modelName ->findOne (null );
1532
+ return $ model ->findOne (null );
1499
1533
1500
1534
} else {
1501
1535
1502
1536
// Keep same query builder from hasMany()
1503
- return $ this -> $ modelName ->findAll (null );
1537
+ return $ model ->findAll (null );
1504
1538
}
1505
1539
1506
1540
}
0 commit comments