2424use think \helper \Arr ;
2525use think \helper \Str ;
2626use think \Model ;
27+ use think \model \Collection ;
2728use think \model \Relation ;
2829use think \model \relation \BelongsTo ;
2930use think \model \relation \BelongsToMany ;
3031use think \model \relation \HasMany ;
3132use think \model \relation \HasManyThrough ;
3233use think \model \relation \HasOne ;
34+ use think \model \relation \HasOneThrough ;
3335use think \model \relation \MorphMany ;
3436use think \model \relation \MorphOne ;
3537use think \model \relation \MorphTo ;
38+ use think \model \relation \MorphToMany ;
3639use Throwable ;
3740
3841class ModelGenerator
@@ -98,12 +101,12 @@ public function addProperty($name, $type = null, $read = null, $write = null, $c
98101 }
99102 }
100103
101- public function addMethod ($ name , $ return = 'mixed ' , $ arguments = [], $ type = ' static ' )
104+ public function addMethod ($ name , $ return = 'mixed ' , $ arguments = [], $ static = true )
102105 {
103106 $ methods = array_change_key_case ($ this ->methods , CASE_LOWER );
104107 if (!isset ($ methods [strtolower ($ name )])) {
105108 $ this ->methods [$ name ] = [];
106- $ this ->methods [$ name ]['type ' ] = $ type ;
109+ $ this ->methods [$ name ]['static ' ] = $ static ? ' static ' : '' ;
107110 $ this ->methods [$ name ]['arguments ' ] = $ arguments ;
108111 $ this ->methods [$ name ]['return ' ] = $ return ;
109112 }
@@ -129,7 +132,7 @@ public function generate()
129132 return ;
130133 }
131134
132- $ this ->model = new ( $ this ->class ) ;
135+ $ this ->model = new $ this ->class ;
133136
134137 $ this ->getPropertiesFromTable ();
135138 $ this ->getPropertiesFromMethods ();
@@ -307,7 +310,7 @@ protected function getPropertiesFromMethods()
307310 if ($ return instanceof Relation) {
308311
309312 $ name = Str::snake ($ methodName );
310- if ($ return instanceof HasOne || $ return instanceof BelongsTo || $ return instanceof MorphOne) {
313+ if ($ return instanceof HasOne || $ return instanceof BelongsTo || $ return instanceof MorphOne || $ return instanceof HasOneThrough ) {
311314 $ this ->addProperty ($ name , "\\" . get_class ($ return ->getModel ()), true , null );
312315 }
313316
@@ -318,6 +321,10 @@ protected function getPropertiesFromMethods()
318321 if ($ return instanceof MorphTo || $ return instanceof MorphMany) {
319322 $ this ->addProperty ($ name , "mixed " , true , null );
320323 }
324+
325+ if ($ return instanceof MorphToMany) {
326+ $ this ->addProperty ($ name , "\\" . Collection::class, true , null );
327+ }
321328 }
322329 } catch (Exception $ e ) {
323330 } catch (Throwable $ e ) {
@@ -405,7 +412,7 @@ protected function createPhpDocs()
405412
406413 $ arguments = implode (', ' , $ method ['arguments ' ]);
407414
408- $ tags [] = $ tagFactory ->create ("@method {$ method ['type ' ]} {$ method ['return ' ]} {$ name }( {$ arguments }) " , $ context );
415+ $ tags [] = $ tagFactory ->create ("@method {$ method ['static ' ]} {$ method ['return ' ]} {$ name }( {$ arguments }) " , $ context );
409416 }
410417
411418 $ tags = $ this ->sortTags ($ tags );
0 commit comments