55use PFinal \Database \Relations \BelongsTo ;
66use PFinal \Database \Relations \HasMany ;
77use PFinal \Database \Relations \HasOne ;
8- use Leaf \ DB ;
8+ use PFinal \ Database \ Builder as DB ;
99use PFinal \Database \Relations \RelationBase ;
1010
1111/**
@@ -47,22 +47,22 @@ trait ModelTrait
4747 */
4848 public static function tableName ()
4949 {
50- return '{{% ' . self ::getTableName (get_called_class ()) . '}} ' ;
50+ return '{{% ' . self ::convertTableName (get_called_class ()) . '}} ' ;
5151 }
5252
5353 public static function __callStatic ($ name , $ arguments )
5454 {
5555 return call_user_func_array (
56- [DB ::table (static ::tableName (), self ::getTableName (get_called_class ()))->asEntity (static :: className ()), $ name ],
56+ [DB ::getInstance ()-> table (static ::tableName (), self ::convertTableName (get_called_class ()))->asEntity (get_called_class ()), $ name ],
5757 $ arguments );
5858 }
5959
6060 public function loadDefaultValues ()
6161 {
62- return DB ::table (static ::tableName ())->loadDefaultValues ($ this );
62+ return DB ::getInstance ()-> table (static ::tableName ())->loadDefaultValues ($ this );
6363 }
6464
65- private static function getTableName ($ className )
65+ private static function convertTableName ($ className )
6666 {
6767 //去掉namespace
6868 $ name = rtrim (str_replace ('\\' , '/ ' , $ className ), '/ \\' );
@@ -87,11 +87,11 @@ private static function getTableName($className)
8787 public function hasOne ($ related , $ foreignKey = null , $ localKey = 'id ' )
8888 {
8989 if ($ foreignKey === null ) {
90- $ foreignKey = self ::getTableName (get_called_class ()) . '_id ' ;
90+ $ foreignKey = self ::convertTableName (get_called_class ()) . '_id ' ;
9191 }
9292
9393 $ hasOne = new HasOne ();
94- $ hasOne ->setConnection (DB ::getConnection ());
94+ $ hasOne ->setConnection (DB ::getInstance ()-> getConnection ());
9595
9696 $ obj = $ hasOne ->table ($ related ::tableName ())->asEntity ($ related );
9797
@@ -113,11 +113,11 @@ public function hasOne($related, $foreignKey = null, $localKey = 'id')
113113 public function belongsTo ($ related , $ foreignKey = null , $ ownerKey = 'id ' )
114114 {
115115 if ($ foreignKey === null ) {
116- $ foreignKey = self ::getTableName ($ related ) . '_id ' ;
116+ $ foreignKey = self ::convertTableName ($ related ) . '_id ' ;
117117 }
118118
119119 $ hasOne = new BelongsTo ();
120- $ hasOne ->setConnection (DB ::getConnection ());
120+ $ hasOne ->setConnection (DB ::getInstance ()-> getConnection ());
121121
122122 $ obj = $ hasOne ->table ($ related ::tableName ())->asEntity ($ related );
123123
@@ -139,11 +139,11 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = 'id')
139139 public function hasMany ($ related , $ foreignKey = null , $ localKey = 'id ' )
140140 {
141141 if ($ foreignKey === null ) {
142- $ foreignKey = self ::getTableName (get_called_class ()) . '_id ' ;
142+ $ foreignKey = self ::convertTableName (get_called_class ()) . '_id ' ;
143143 }
144144
145145 $ hasOne = new HasMany ();
146- $ hasOne ->setConnection (DB ::getConnection ());
146+ $ hasOne ->setConnection (DB ::getInstance ()-> getConnection ());
147147
148148 $ obj = $ hasOne ->table ($ related ::tableName ())->asEntity ($ related );
149149
@@ -167,7 +167,7 @@ public static function with($relations)
167167 {
168168 $ relations = is_string ($ relations ) ? func_get_args () : $ relations ;
169169
170- return DB ::table (static ::tableName (), self ::getTableName (get_called_class ()))->asEntity (static :: className ())->afterFind (function ($ models ) use ($ relations ) {
170+ return DB ::getInstance ()-> table (static ::tableName (), self ::convertTableName (get_called_class ()))->asEntity (get_called_class ())->afterFind (function ($ models ) use ($ relations ) {
171171 RelationBase::appendRelationData ($ models , $ relations );
172172 });
173173 }
0 commit comments