@@ -107,8 +107,20 @@ class BeanDescriptor implements BeanDescriptorInterface
107
107
*/
108
108
private $ configuration ;
109
109
110
- public function __construct (Table $ table , string $ beanNamespace , string $ generatedBeanNamespace , string $ daoNamespace , string $ generatedDaoNamespace , SchemaAnalyzer $ schemaAnalyzer , Schema $ schema , TDBMSchemaAnalyzer $ tdbmSchemaAnalyzer , NamingStrategyInterface $ namingStrategy , AnnotationParser $ annotationParser , CodeGeneratorListenerInterface $ codeGeneratorListener , ConfigurationInterface $ configuration )
111
- {
110
+ public function __construct (
111
+ Table $ table ,
112
+ string $ beanNamespace ,
113
+ string $ generatedBeanNamespace ,
114
+ string $ daoNamespace ,
115
+ string $ generatedDaoNamespace ,
116
+ SchemaAnalyzer $ schemaAnalyzer ,
117
+ Schema $ schema ,
118
+ TDBMSchemaAnalyzer $ tdbmSchemaAnalyzer ,
119
+ NamingStrategyInterface $ namingStrategy ,
120
+ AnnotationParser $ annotationParser ,
121
+ CodeGeneratorListenerInterface $ codeGeneratorListener ,
122
+ ConfigurationInterface $ configuration
123
+ ) {
112
124
$ this ->table = $ table ;
113
125
$ this ->beanNamespace = $ beanNamespace ;
114
126
$ this ->generatedBeanNamespace = $ generatedBeanNamespace ;
@@ -360,7 +372,7 @@ private function getDirectForeignKeysDescriptors(): array
360
372
$ descriptors = [];
361
373
362
374
foreach ($ fks as $ fk ) {
363
- $ descriptors [] = new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser );
375
+ $ descriptors [] = new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser , $ this -> beanNamespace );
364
376
}
365
377
366
378
return $ descriptors ;
@@ -795,27 +807,35 @@ public function generateDaoPhpCode(): ?FileGenerator
795
807
$ class ->addMethodFromGenerator ($ findAllMethod );
796
808
}
797
809
798
- if (count ($ primaryKeyColumns ) === 1 ) {
799
- $ primaryKeyColumn = $ primaryKeyColumns [0 ];
800
- $ primaryKeyPhpType = TDBMDaoGenerator::dbalTypeToPhpType ($ this ->table ->getColumn ($ primaryKeyColumn )->getType ());
810
+ if (count ($ primaryKeyColumns ) > 0 ) {
811
+ $ lazyLoadingParameterName = 'lazyLoading ' ;
812
+ $ parameters = [];
813
+ $ parametersTag = [];
814
+ $ primaryKeyFilter = [];
815
+
816
+ foreach ($ primaryKeyColumns as $ primaryKeyColumn ) {
817
+ if ($ primaryKeyColumn === $ lazyLoadingParameterName ) {
818
+ throw new TDBMException ('Primary Column name ` ' . $ lazyLoadingParameterName . '` is not allowed. ' );
819
+ }
820
+ $ phpType = TDBMDaoGenerator::dbalTypeToPhpType ($ this ->table ->getColumn ($ primaryKeyColumn )->getType ());
821
+ $ parameters [] = new ParameterGenerator ($ primaryKeyColumn , $ phpType );
822
+ $ parametersTag [] = new ParamTag ($ primaryKeyColumn , [$ phpType ]);
823
+ $ primaryKeyFilter [] = "' $ primaryKeyColumn' => \$$ primaryKeyColumn " ;
824
+ }
825
+ $ parameters [] = new ParameterGenerator ($ lazyLoadingParameterName , 'bool ' , false );
826
+ $ parametersTag [] = new ParamTag ($ lazyLoadingParameterName , ['bool ' ], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object. ' );
827
+ $ parametersTag [] = new ReturnTag (['\\' .$ beanClassName ]);
828
+ $ parametersTag [] = new ThrowsTag ('\\' .TDBMException::class);
801
829
802
830
$ getByIdMethod = new MethodGenerator (
803
831
'getById ' ,
804
- [
805
- new ParameterGenerator ('id ' , $ primaryKeyPhpType ),
806
- new ParameterGenerator ('lazyLoading ' , 'bool ' , false )
807
- ],
832
+ $ parameters ,
808
833
MethodGenerator::FLAG_PUBLIC ,
809
- "return \$this->tdbmService->findObjectByPk(' $ tableName', [' $ primaryKeyColumn ' => \$ id ], [], \$lazyLoading ); " ,
834
+ "return \$this->tdbmService->findObjectByPk(' $ tableName', [ " . implode ( ' , ' , $ primaryKeyFilter ) . " ], [], \$$ lazyLoadingParameterName ); " ,
810
835
(new DocBlockGenerator (
811
836
"Get $ beanClassWithoutNameSpace specified by its ID (its primary key). " ,
812
837
'If the primary key does not exist, an exception is thrown. ' ,
813
- [
814
- new ParamTag ('id ' , [$ primaryKeyPhpType ]),
815
- new ParamTag ('lazyLoading ' , ['bool ' ], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object. ' ),
816
- new ReturnTag (['\\' .$ beanClassName ]),
817
- new ThrowsTag ('\\' .TDBMException::class)
818
- ]
838
+ $ parametersTag
819
839
))->setWordWrap (false )
820
840
);
821
841
$ getByIdMethod ->setReturnType ($ beanClassName );
@@ -1118,6 +1138,9 @@ private function generateFindByDaoCode(string $beanNamespace, string $beanClassN
1118
1138
}
1119
1139
}
1120
1140
}
1141
+ usort ($ methods , static function (MethodGenerator $ methodA , MethodGenerator $ methodB ) {
1142
+ return $ methodA ->getName () <=> $ methodB ->getName ();
1143
+ });
1121
1144
1122
1145
return $ methods ;
1123
1146
}
@@ -1132,7 +1155,11 @@ private function removeDuplicateIndexes(array $indexes): array
1132
1155
{
1133
1156
$ indexesByKey = [];
1134
1157
foreach ($ indexes as $ index ) {
1135
- $ indexesByKey [implode ('__`__ ' , $ index ->getUnquotedColumns ())] = $ index ;
1158
+ $ key = implode ('__`__ ' , $ index ->getUnquotedColumns ());
1159
+ // Unique Index have precedence over non unique one
1160
+ if (!isset ($ indexesByKey [$ key ]) || $ index ->isUnique ()) {
1161
+ $ indexesByKey [$ key ] = $ index ;
1162
+ }
1136
1163
}
1137
1164
1138
1165
return array_values ($ indexesByKey );
@@ -1175,9 +1202,10 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
1175
1202
$ parameters = [];
1176
1203
//$functionParameters = [];
1177
1204
$ first = true ;
1205
+ /** @var AbstractBeanPropertyDescriptor $element */
1178
1206
foreach ($ elements as $ element ) {
1179
1207
$ parameter = new ParameterGenerator (ltrim ($ element ->getVariableName (), '$ ' ));
1180
- if (!$ first ) {
1208
+ if (!$ first && !( $ element -> isCompulsory () && $ index -> isUnique ()) ) {
1181
1209
$ parameterType = '? ' ;
1182
1210
//$functionParameter = '?';
1183
1211
} else {
@@ -1186,7 +1214,7 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
1186
1214
}
1187
1215
$ parameterType .= $ element ->getPhpType ();
1188
1216
$ parameter ->setType ($ parameterType );
1189
- if (!$ first ) {
1217
+ if (!$ first && !( $ element -> isCompulsory () && $ index -> isUnique ()) ) {
1190
1218
$ parameter ->setDefaultValue (null );
1191
1219
}
1192
1220
//$functionParameter .= $element->getPhpType();
@@ -1224,7 +1252,7 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
1224
1252
foreach ($ columns as $ localColumn => $ foreignColumn ) {
1225
1253
// TODO: a foreign key could point to another foreign key. In this case, there is no getter for the pointed column. We don't support this case.
1226
1254
$ targetedElement = new ScalarBeanPropertyDescriptor ($ foreignTable , $ foreignTable ->getColumn ($ foreignColumn ), $ this ->namingStrategy , $ this ->annotationParser );
1227
- if ($ first ) {
1255
+ if ($ first || $ element -> isCompulsory () && $ index -> isUnique () ) {
1228
1256
// First parameter for index is not nullable
1229
1257
$ filterArrayCode .= ' ' .var_export ($ localColumn , true ).' => ' .$ element ->getVariableName ().'-> ' .$ targetedElement ->getGetterName ()."(), \n" ;
1230
1258
} else {
0 commit comments