@@ -37,31 +37,30 @@ public function __construct(
3737
3838 private function writeFunction (string $ functionStub , string $ functionName , string $ columnName , string $ attributeType ): string
3939 {
40+ $ columnNameSingle = Str::camel ($ columnName );
41+
4042 if ($ functionName === 'getOneBy ' ) {
4143 $ functionReturnType = 'null|{{ EntityName }} ' ;
4244 $ functionName .= ucfirst (Str::camel ($ columnName ));
4345 $ columnName = Str::camel ($ columnName );
4446 $ redisCashFunction = $ this ->getRedisCashFunctionGetOneBy ($ this ->strategyName );
45-
46-
4747 } elseif ($ functionName === 'getAllBy ' ) {
4848 $ functionReturnType = 'Collection ' ;
4949 $ functionName .= ucfirst (Str::plural (Str::camel ($ columnName )));
5050 $ columnName = Str::plural (Str::camel ($ columnName ));
5151 $ redisCashFunction = $ this ->getRedisCashFunctionGetAllBy ($ this ->strategyName );
52-
53-
5452 } elseif ($ functionName === 'create ' ) {
5553 $ functionReturnType = $ attributeType ;
5654 $ redisCashFunction = $ this ->getRedisCashFunctionCreate ($ this ->strategyName );
57-
5855 } elseif (in_array ($ functionName , ['update ' , 'remove ' , 'restore ' ])) {
5956 $ functionReturnType = 'int ' ;
6057 $ redisCashFunction = $ this ->getRedisCashFunctionUpdate ($ this ->strategyName );
61-
6258 }
59+
60+ $ redisCashFunction = str_replace (['{{ FunctionName }} ' , '{{ ColumnName }} ' , '{{ ColumnNameSingle }} ' ], [$ functionName , $ columnName , $ columnNameSingle ], $ redisCashFunction );
61+
6362 return str_replace (['{{ FunctionName }} ' , '{{ AttributeType }} ' , '{{ AttributeName }} ' , '{{ FunctionReturnType }} ' ,'{{redisFunction}} ' ],
64- [$ functionName , $ attributeType , Str::camel ($ columnName ), $ functionReturnType ,$ redisCashFunction ],
63+ [$ functionName , $ attributeType , Str::camel ($ columnName ), $ functionReturnType , $ redisCashFunction ],
6564 $ functionStub );
6665 }
6766 private function writeSqlAttribute (string $ attributeStub , string $ sqlRepositoryVariable , string $ sqlRepositoryName ): string
@@ -119,12 +118,18 @@ public function createFunctions(): array
119118 $ functions ['__construct ' ] = $ this ->getConstructRedis ($ setterSqlStub , $ constructStub );
120119 $ functions ['getOneById ' ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , 'id ' , 'int ' );
121120 $ functions ['getAllByIds ' ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , 'id ' , 'array ' );
121+ $ columnsInfo = $ this ->getAllColumnsInTable ($ this ->tableName );
122+
122123 $ indexes = $ this ->extractIndexes ($ this ->tableName );
123124 foreach ($ indexes as $ index ) {
124- $ fun_name = ucfirst (Str::plural (Str::camel ($ index ->COLUMN_NAME )));
125- $ functions ['getAllBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ index ->COLUMN_NAME , 'array ' );
125+ $ columnInfo = collect ($ columnsInfo )->where ('COLUMN_NAME ' , $ index ->COLUMN_NAME )->first ();
126126 $ fun_name = ucfirst (Str::camel ($ index ->COLUMN_NAME ));
127- $ functions ['getOneBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ index ->COLUMN_NAME , 'int ' );
127+ $ functions ['getOneBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE ));
128+
129+ if ($ index ->Non_unique == 1 ) {
130+ $ fun_name = ucfirst (Str::plural (Str::camel ($ index ->COLUMN_NAME )));
131+ $ functions ['getAllBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ index ->COLUMN_NAME , 'array ' );
132+ }
128133 }
129134 if ($ this ->detectForeignKeys ) {
130135 $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
0 commit comments