@@ -17,14 +17,15 @@ public function handle()
17
17
$ modelB = $ this ->argument ('modelB ' );
18
18
$ relation = $ this ->argument ('relation ' );
19
19
20
- //$this->info(' > Checking ' . $this->qualifyModel($modelA));
20
+ // If Model A exists
21
21
if (file_exists ($ this ->getPath ($ this ->qualifyClass ($ modelA )))) {
22
- // 代表是把原本有的東東新增
22
+ // Check if relation function (likes "cars" or "car")
23
23
if (method_exists ($ this ->qualifyClass ($ modelA ), strtolower ($ modelB )) || method_exists ($ this ->qualifyClass ($ modelA ), strtolower ($ modelB ) . 's ' )) {
24
24
$ this ->error ('Method exists. ' );
25
-
26
25
return 1 ;
27
26
}
27
+
28
+ // [A1] Adding relation function
28
29
$ content = file_get_contents ($ this ->getPath ($ this ->qualifyClass ($ modelA )));
29
30
$ str = $ this ->str_lreplace ('} ' , (<<<FUNC
30
31
public function {{ modelName }}(){
@@ -34,13 +35,15 @@ public function {{ modelName }}(){
34
35
FUNC ), $ content );
35
36
file_put_contents ($ this ->getPath ($ this ->qualifyClass ($ modelA )), $ str );
36
37
$ this ->info (' # Modified ' . $ this ->qualifyClass ($ modelA ));
38
+ // [/A1]
37
39
} else {
40
+ // Model A not exists => call parent create model files
38
41
parent ::handle ();
39
42
$ this ->info (' # Created ' . $ this ->qualifyClass ($ modelA ));
40
43
}
41
44
45
+ // Replace model name (if model exists, then it'll not doing so.)
42
46
$ content = file_get_contents ($ this ->getPath ($ this ->qualifyClass ($ modelA )));
43
-
44
47
$ str = str_replace ('{{ model }} ' , $ modelB , $ content );
45
48
46
49
if ($ relation === 'hasMany ' ) {
@@ -112,7 +115,14 @@ public function {{ modelName }}(){
112
115
############
113
116
## Normal ##
114
117
############
115
- $ arr = [$ modelA ,$ modelB ];
118
+ $ arr = [];
119
+ if (!file_exists ($ this ->getPath ($ this ->qualifyClass ($ modelA )))){
120
+ array_push ($ arr , $ modelA );
121
+ }
122
+ if (!file_exists ($ this ->getPath ($ this ->qualifyClass ($ modelB )))){
123
+ array_push ($ arr , $ modelB );
124
+ }
125
+
116
126
foreach ($ arr as $ model ) {
117
127
$ file = file_get_contents ($ this ->getParsedStub ('migration.relation.stub ' ));
118
128
$ this ->clearMigration ();
0 commit comments