@@ -260,7 +260,7 @@ protected function establishConnection()
260260 $ connection = Connection::getFromContainer ();
261261 $ name = $ this ->connection ;
262262
263- if (!is_string ( $ name) || ! $ connection -> exists ( $ name ) ) {
263+ if (!$ name ) {
264264 return $ connection ->default ();
265265 }
266266
@@ -1967,15 +1967,14 @@ public function getCast( $property )
19671967 */
19681968 public function hasOne ($ modelClass , $ id_foreign = null , $ scope = null )
19691969 {
1970- $ id = $ this ->getID ();
1970+ /** @var Model $relationship */
1971+ $ relationship = new $ modelClass ;
1972+ $ relationship ->setRelatedModel ( $ this );
19711973
19721974 if ( ! $ id_foreign && $ this ->resource ) {
19731975 $ id_foreign = $ this ->resource . '_id ' ;
19741976 }
19751977
1976- /** @var Model $relationship */
1977- $ relationship = new $ modelClass ;
1978- $ relationship ->setRelatedModel ( $ this );
19791978 $ relationship ->relatedBy = [
19801979 'type ' => 'hasOne ' ,
19811980 'query ' => [
@@ -1986,7 +1985,12 @@ public function hasOne($modelClass, $id_foreign = null, $scope = null)
19861985 ]
19871986 ];
19881987
1989- return $ relationship ->findAll ()->where ( $ id_foreign , $ id )->take (1 );
1988+ if (is_callable ($ scope )) {
1989+ $ scope ($ relationship );
1990+ }
1991+
1992+ $ id = $ this ->getID ();
1993+ return $ relationship ->where ( $ id_foreign , $ id )->take (1 );
19901994 }
19911995
19921996 /**
@@ -2003,6 +2007,11 @@ public function belongsTo($modelClass, $id_local = null, $scope = null)
20032007 /** @var Model $relationship */
20042008 $ relationship = new $ modelClass ;
20052009 $ relationship ->setRelatedModel ( $ this );
2010+
2011+ if ( ! $ id_local && $ relationship ->resource ) {
2012+ $ id_local = $ relationship ->resource . '_id ' ;
2013+ }
2014+
20062015 $ relationship ->relatedBy = [
20072016 'type ' => 'belongsTo ' ,
20082017 'query ' => [
@@ -2013,8 +2022,8 @@ public function belongsTo($modelClass, $id_local = null, $scope = null)
20132022 ]
20142023 ];
20152024
2016- if ( ! $ id_local && $ relationship -> resource ) {
2017- $ id_local = $ relationship-> resource . ' _id ' ;
2025+ if (is_callable ( $ scope ) ) {
2026+ $ scope ( $ relationship) ;
20182027 }
20192028
20202029 $ id = $ this ->getProperty ( $ id_local );
0 commit comments