@@ -173,7 +173,7 @@ class Model implements Formable, JsonSerializable
173173 /**
174174 * @var array
175175 */
176- #[ArrayShape(['type ' => 'string ' , 'query ' => 'array ' ])]
176+ #[ArrayShape(['type ' => 'string ' , 'query ' => 'array ' , ' where_on ' => ' array ' ])]
177177 protected $ relatedBy ;
178178
179179 /**
@@ -1661,7 +1661,6 @@ public function whereRelationship(string $relationship, bool $exists, $scope = n
16611661 throw new \Exception ("Trying to get relationship of ' {$ relationship }' but no Model class is returned for " . get_class ($ this ));
16621662 }
16631663
1664- $ junction = $ rel ->getJunction ();
16651664 $ related = $ rel ->getRelatedBy ();
16661665 $ id_column = null ;
16671666 $ where_on_index = -1 ;
@@ -2052,12 +2051,14 @@ public function getCast( $property )
20522051 /**
20532052 * Has One
20542053 *
2055- * @param string $modelClass
2054+ * @template M
2055+ *
2056+ * @param class-string<M> $modelClass
20562057 * @param null|string $id_foreign
20572058 * @param null|string|callable $id_local
20582059 * @param null|callable $scope
20592060 *
2060- * @return null|Model
2061+ * @return null|Model|M
20612062 */
20622063 public function hasOne ($ modelClass , $ id_foreign = null , $ id_local = null , $ scope = null )
20632064 {
@@ -2091,8 +2092,12 @@ public function hasOne($modelClass, $id_foreign = null, $id_local = null, $scope
20912092 $ scope ($ relationship );
20922093 }
20932094
2095+ $ rel_table = $ relationship ->getTable ();
2096+ $ id_foreign = $ id_foreign ?? $ relationship ->getIdColumn ();
2097+ $ id_foreign_where = str_contains ($ id_foreign , '. ' ) ? $ id_foreign : "{$ rel_table }. $ id_foreign " ;
2098+
20942099 $ id = $ this ->getPropertyValueDirect ($ id_local );
2095- $ result = $ relationship ->where ( $ id_foreign , $ id )->take (1 );
2100+ $ result = $ relationship ->where ($ id_foreign_where , $ id )->take (1 );
20962101 $ relationship ->relatedBy ['where_on ' ] = $ relationship ->getQuery ()->lastWhere ();
20972102
20982103 return $ result ;
@@ -2101,12 +2106,14 @@ public function hasOne($modelClass, $id_foreign = null, $id_local = null, $scope
21012106 /**
21022107 * Belongs To
21032108 *
2104- * @param string $modelClass
2109+ * @template M
2110+ *
2111+ * @param class-string<M> $modelClass
21052112 * @param null|string $id_local
21062113 * @param null|string|callable $id_foreign
21072114 * @param null|callable $scope
21082115 *
2109- * @return null|Model
2116+ * @return null|Model|M
21102117 */
21112118 public function belongsTo ($ modelClass , $ id_local = null , $ id_foreign = null , $ scope = null )
21122119 {
@@ -2119,6 +2126,8 @@ public function belongsTo($modelClass, $id_local = null, $id_foreign = null, $sc
21192126 $ id_foreign = $ relationship ->getIdColumn ();
21202127 } elseif (is_null ($ id_foreign ) && is_null ($ scope )) {
21212128 $ id_foreign = $ relationship ->getIdColumn ();
2129+ } else {
2130+ $ id_foreign = $ id_foreign ?? $ relationship ->getIdColumn ();
21222131 }
21232132
21242133 if ( ! $ id_local && $ relationship ->resource ) {
@@ -2141,7 +2150,11 @@ public function belongsTo($modelClass, $id_local = null, $id_foreign = null, $sc
21412150 }
21422151
21432152 $ id = $ this ->getProperty ( $ id_local );
2144- $ result = $ relationship ->where ( $ id_foreign ?? $ relationship ->getIdColumn (), $ id )->take (1 );
2153+
2154+ $ rel_table = $ relationship ->getTable ();
2155+ $ id_foreign_where = str_contains ($ id_foreign , '. ' ) ? $ id_foreign : "{$ rel_table }. $ id_foreign " ;
2156+
2157+ $ result = $ relationship ->where ($ id_foreign_where , $ id )->take (1 );
21452158 $ relationship ->relatedBy ['where_on ' ] = $ relationship ->getQuery ()->lastWhere ();
21462159
21472160 return $ result ;
@@ -2150,12 +2163,14 @@ public function belongsTo($modelClass, $id_local = null, $id_foreign = null, $sc
21502163 /**
21512164 * Has Many
21522165 *
2153- * @param string $modelClass
2166+ * @template M
2167+ *
2168+ * @param class-string<M> $modelClass
21542169 * @param null|string $id_foreign
21552170 * @param null|string|callable $id_local
21562171 * @param null|callable $scope
21572172 *
2158- * @return null|Model
2173+ * @return null|Model|M
21592174 */
21602175 public function hasMany ($ modelClass , $ id_foreign = null , $ id_local = null , $ scope = null )
21612176 {
@@ -2190,7 +2205,10 @@ public function hasMany($modelClass, $id_foreign = null, $id_local = null, $scop
21902205 $ scope ($ relationship );
21912206 }
21922207
2193- $ result = $ relationship ->findAll ()->where ( $ id_foreign , $ id );
2208+ $ rel_table = $ relationship ->getTable ();
2209+ $ id_foreign_where = str_contains ($ id_foreign , '. ' ) ? $ id_foreign : "{$ rel_table }. $ id_foreign " ;
2210+
2211+ $ result = $ relationship ->findAll ()->where ( $ id_foreign_where , $ id );
21942212 $ relationship ->relatedBy ['where_on ' ] = $ relationship ->getQuery ()->lastWhere ();
21952213
21962214 return $ result ;
@@ -2201,14 +2219,16 @@ public function hasMany($modelClass, $id_foreign = null, $id_local = null, $scop
22012219 *
22022220 * This is for Many-to-Many relationships.
22032221 *
2204- * @param string|array $modelClass
2222+ * @template M
2223+ *
2224+ * @param class-string<M> $modelClass
22052225 * @param string $junction_table
22062226 * @param null|string $id_column
22072227 * @param null|string $id_foreign
22082228 * @param null|callable $scope
22092229 * @param bool $reselect
22102230 *
2211- * @return null|Model
2231+ * @return null|Model|M
22122232 */
22132233 public function belongsToMany ( $ modelClass , $ junction_table , $ id_column = null , $ id_foreign = null , $ scope = null , $ reselect = true , $ id_local = null , $ id_foreign_local = null )
22142234 {
@@ -2293,7 +2313,7 @@ public function belongsToMany( $modelClass, $junction_table, $id_column = null,
22932313 *
22942314 * @param array $args
22952315 *
2296- * @return array $query
2316+ * @return array{0: mixed, 1: Query}
22972317 */
22982318 public function attach ( array $ args )
22992319 {
@@ -2329,7 +2349,7 @@ public function attach( array $args )
23292349 *
23302350 * @param array $args
23312351 *
2332- * @return array
2352+ * @return array{0: mixed, 1: Query}
23332353 */
23342354 public function detach ( array $ args = [] )
23352355 {
@@ -2357,7 +2377,7 @@ public function detach( array $args = [] )
23572377 *
23582378 * @param array $args
23592379 *
2360- * @return array $results
2380+ * @return array{detach: array{0: mixed, 1: Query}, attach: array{0: mixed, 1: Query}}
23612381 */
23622382 public function sync ( array $ args = [] )
23632383 {
@@ -2371,13 +2391,13 @@ public function sync( array $args = [] )
23712391 /**
23722392 * Get Table
23732393 *
2374- * @return null
2394+ * @return string
23752395 */
23762396 public function getTable ()
23772397 {
23782398 $ connection = $ this ->query ->getWpdb ();
23792399
2380- return $ this ->table ?: $ connection ->prefix . $ this ->resource ;
2400+ return $ this ->table ?: $ connection ->prefix . $ this ->resource ;
23812401 }
23822402
23832403 /**
0 commit comments