@@ -142,6 +142,32 @@ function &_getModelObject($className) {
142
142
return $ object ;
143
143
}
144
144
145
+ /**
146
+ * Generate a key value list of options and a prompt.
147
+ *
148
+ * @param array $options Array of options to use for the selections. indexes must start at 0
149
+ * @param string $prompt Prompt to use for options list.
150
+ * @param integer $default The default option for the given prompt.
151
+ * @return result of user choice.
152
+ **/
153
+ function inOptions ($ options , $ prompt = null , $ default = null ) {
154
+ $ valid = false ;
155
+ $ max = count ($ options );
156
+ while (!$ valid ) {
157
+ foreach ($ options as $ i => $ option ) {
158
+ $ this ->out ($ i + 1 .'. ' . $ option );
159
+ }
160
+ if (empty ($ prompt )) {
161
+ $ prompt = __ ('Make a selection from the choices above ' , true );
162
+ }
163
+ $ choice = $ this ->in ($ prompt , null , $ default );
164
+ if (intval ($ choice ) > 0 && intval ($ choice ) <= $ max ) {
165
+ $ valid = true ;
166
+ }
167
+ }
168
+ return $ choice - 1 ;
169
+ }
170
+
145
171
/**
146
172
* Handles interactive baking
147
173
*
@@ -578,28 +604,24 @@ function doMoreAssociations($model, $associations) {
578
604
$ wannaDoMoreAssoc = $ this ->in ($ prompt , array ('y ' ,'n ' ), 'n ' );
579
605
$ possibleKeys = $ this ->_generatePossibleKeys ();
580
606
while (low ($ wannaDoMoreAssoc ) == 'y ' ) {
581
- $ assocs = array (1 => 'belongsTo ' , 2 => 'hasOne ' , 3 => 'hasMany ' , 4 => 'hasAndBelongsToMany ' );
607
+ $ assocs = array ('belongsTo ' , 'hasOne ' , 'hasMany ' , 'hasAndBelongsToMany ' );
582
608
$ this ->out (__ ('What is the association type? ' , true ));
583
- $ prompt = "1. belongsTo \n" ;
584
- $ prompt .= "2. hasOne \n" ;
585
- $ prompt .= "3. hasMany \n" ;
586
- $ prompt .= "4. hasAndBelongsToMany \n" ;
587
- $ assocType = intval ($ this ->in ($ prompt , array_keys ($ assocs ), __ ("Enter a number " , true )));
588
-
609
+ $ assocType = intval ($ this ->inOptions ($ assocs , __ ('Enter a number ' ,true )));
610
+
589
611
$ this ->out (__ ("For the following options be very careful to match your setup exactly. \nAny spelling mistakes will cause errors. " , true ));
590
612
$ this ->hr ();
591
613
592
614
$ alias = $ this ->in (__ ('What is the alias for this association? ' , true ));
593
615
$ className = $ this ->in (sprintf (__ ('What className will %s use? ' , true ), $ alias ), null , $ alias );
594
616
$ suggestedForeignKey = null ;
595
617
596
- if ($ assocType == ' 1 ' ) {
618
+ if ($ assocType == 0 ) {
597
619
$ showKeys = $ possibleKeys [$ model ->table ];
598
620
$ suggestedForeignKey = $ this ->_modelKey ($ alias );
599
621
} else {
600
622
$ otherTable = Inflector::tableize ($ className );
601
623
if (in_array ($ otherTable , $ this ->__tables )) {
602
- if ($ assocType < ' 4 ' ) {
624
+ if ($ assocType < 3 ) {
603
625
$ showKeys = $ possibleKeys [$ otherTable ];
604
626
} else {
605
627
$ showKeys = null ;
@@ -612,18 +634,13 @@ function doMoreAssociations($model, $associations) {
612
634
}
613
635
if (!empty ($ showKeys )) {
614
636
$ this ->out (__ ('A helpful List of possible keys ' , true ));
615
- for ($ i = 0 ; $ i < count ($ showKeys ); $ i ++) {
616
- $ this ->out ($ i + 1 . ". " . $ showKeys [$ i ]);
617
- }
618
- $ foreignKey = $ this ->in (__ ('What is the foreignKey? ' , true ), null , __ ("Enter a number " , true ));
619
- if (intval ($ foreignKey ) > 0 && intval ($ foreignKey ) <= $ i ) {
620
- $ foreignKey = $ showKeys [intval ($ foreignKey ) - 1 ];
621
- }
637
+ $ foreignKey = $ this ->inOptions ($ showKeys , __ ('What is the foreignKey? ' , true ));
638
+ $ foreignKey = $ showKeys [intval ($ foreignKey )];
622
639
}
623
640
if (!isset ($ foreignKey )) {
624
641
$ foreignKey = $ this ->in (__ ('What is the foreignKey? Specify your own. ' , true ), null , $ suggestedForeignKey );
625
642
}
626
- if ($ assocType == ' 4 ' ) {
643
+ if ($ assocType == 3 ) {
627
644
$ associationForeignKey = $ this ->in (__ ('What is the associationForeignKey? ' , true ), null , $ this ->_modelKey ($ model ->name ));
628
645
$ joinTable = $ this ->in (__ ('What is the joinTable? ' , true ));
629
646
}
@@ -633,7 +650,7 @@ function doMoreAssociations($model, $associations) {
633
650
$ associations [$ assocs [$ assocType ]][$ i ]['alias ' ] = $ alias ;
634
651
$ associations [$ assocs [$ assocType ]][$ i ]['className ' ] = $ className ;
635
652
$ associations [$ assocs [$ assocType ]][$ i ]['foreignKey ' ] = $ foreignKey ;
636
- if ($ assocType == ' 4 ' ) {
653
+ if ($ assocType == 3 ) {
637
654
$ associations [$ assocs [$ assocType ]][$ i ]['associationForeignKey ' ] = $ associationForeignKey ;
638
655
$ associations [$ assocs [$ assocType ]][$ i ]['joinTable ' ] = $ joinTable ;
639
656
}
0 commit comments