@@ -39,11 +39,11 @@ class MysqlParser extends ParserBase
3939 protected $ largeDataTypes = ['varbinary ' , 'blob ' , 'mediumblob ' , 'longblob ' , 'text ' , 'mediumtext ' , 'longtext ' ];
4040
4141 /**
42- * Gets column meta info from the information schema.
42+ * Gets columns meta info from the information schema.
4343 *
4444 * @return array
4545 */
46- protected function getColumn ()
46+ protected function getColumns ()
4747 {
4848 return DB ::select (
4949 'SELECT
@@ -94,6 +94,7 @@ protected function getConstraints()
9494 ,r.DELETE_RULE AS `onDelete`
9595 ,u.referenced_column_name AS `on`
9696 ,u.column_name AS `foreign`
97+ ,CASE WHEN u.TABLE_NAME = r.referenced_table_name THEN 1 ELSE 0 END selfReferences
9798 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS r
9899 INNER JOIN information_schema.key_column_usage AS u ON u.CONSTRAINT_NAME = r.CONSTRAINT_NAME
99100 AND u.table_schema = r.constraint_schema
@@ -132,9 +133,8 @@ protected function getRelations()
132133 {
133134 $ relations = [];
134135 $ rawRelations = $ this ->getRawRelations ();
135-
136136 foreach ($ rawRelations as $ rawRelation ) {
137- $ relations [] = $ this ->getRealtion ($ rawRelation ->foreignTable , $ rawRelation ->foreignKey , $ rawRelation ->localKey );
137+ $ relations [] = $ this ->getRealtion ($ rawRelation ->foreignTable , $ rawRelation ->foreignKey , $ rawRelation ->localKey , $ rawRelation -> selfReferences );
138138 }
139139
140140 return $ relations ;
@@ -153,6 +153,7 @@ protected function getRawRelations()
153153 u.referenced_column_name AS `localKey`
154154 ,u.column_name AS `foreignKey`
155155 ,r.table_name AS `foreignTable`
156+ ,CASE WHEN u.TABLE_NAME = r.referenced_table_name THEN 1 ELSE 0 END selfReferences
156157 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS r
157158 INNER JOIN information_schema.key_column_usage AS u ON u.CONSTRAINT_NAME = r.CONSTRAINT_NAME
158159 AND u.table_schema = r.constraint_schema
@@ -184,22 +185,24 @@ protected function getRelationTypeQuery($tableName, $columnName)
184185 *
185186 * @return CrestApps\CodeGenerator\Models\ForeignRelationship
186187 */
187- protected function getRealtion ($ foreignTableName , $ foreignColumn , $ localColumn )
188+ protected function getRealtion ($ foreignTableName , $ foreignColumn , $ localColumn, $ selfReferences )
188189 {
189190 $ modelName = $ this ->getModelName ($ foreignTableName );
190- $ model = Helpers::guessModelFullName ($ modelName , $ this -> getModelNamespace ());
191+ $ model = Helpers::guessModelFullName ($ modelName , Helpers:: getModelsPath ());
191192
192193 $ params = [
193194 $ model ,
194195 $ foreignColumn ,
195196 $ localColumn ,
196197 ];
197198
199+ $ relationName = ($ selfReferences ? 'child_ ' : '' );
200+
198201 if ($ this ->isOneToMany ($ foreignTableName , $ foreignColumn )) {
199- return new ForeignRelationship ('hasMany ' , $ params , camel_case (Str::plural ($ foreignTableName )));
202+ return new ForeignRelationship ('hasMany ' , $ params , camel_case ($ relationName . Str::plural ($ foreignTableName )));
200203 }
201204
202- return new ForeignRelationship ('hasOne ' , $ params , camel_case (Str::singular ($ foreignTableName )));
205+ return new ForeignRelationship ('hasOne ' , $ params , camel_case ($ relationName . Str::singular ($ foreignTableName )));
203206 }
204207
205208 /**
@@ -255,7 +258,7 @@ protected function getTransfredFields(array $columns)
255258 $ properties ['data-value ' ] = $ column ->COLUMN_DEFAULT ;
256259 $ properties ['data-type ' ] = $ this ->getDataType ($ column ->DATA_TYPE , $ column ->COLUMN_TYPE );
257260 $ properties ['data-type-params ' ] = $ this ->getPrecision ($ column ->CHARACTER_MAXIMUM_LENGTH , $ column ->DATA_TYPE , $ column ->COLUMN_TYPE );
258- $ properties ['is-primary ' ] = ($ column ->COLUMN_KEY == 'PRIMARY KEY ' );
261+ $ properties ['is-primary ' ] = in_array ($ column ->COLUMN_KEY , [ 'PRIMARY KEY ' , ' PRI ' ] );
259262 $ properties ['is-index ' ] = ($ column ->COLUMN_KEY == 'MUL ' );
260263 $ properties ['is-unique ' ] = ($ column ->COLUMN_KEY == 'UNI ' );
261264 $ properties ['is-auto-increment ' ] = ($ column ->EXTRA == 'AUTO_INCREMENT ' );
@@ -274,7 +277,9 @@ protected function getTransfredFields(array $columns)
274277
275278 $ collection [] = $ properties ;
276279 }
280+
277281 $ localeGroup = Helpers::makeLocaleGroup ($ this ->tableName );
282+
278283 $ fields = FieldTransformer::fromArray ($ collection , $ localeGroup , $ this ->languages );
279284
280285 // At this point we constructed the fields collection with the default html-type
@@ -353,11 +358,13 @@ protected function getForeignConstraint($name)
353358 }
354359
355360 return new ForeignConstraint (
356- strtolower ( $ raw ->foreign ) ,
357- strtolower ( $ raw ->references ) ,
358- strtolower ( $ raw ->on ) ,
361+ $ raw ->foreign ,
362+ $ raw ->references ,
363+ $ raw ->on ,
359364 strtolower ($ raw ->onDelete ),
360- strtolower ($ raw ->onUpdate )
365+ strtolower ($ raw ->onUpdate ),
366+ null ,
367+ $ raw ->selfReferences
361368 );
362369 }
363370
0 commit comments