@@ -548,6 +548,8 @@ protected function writeRelationsVar(WriterInterface $writer)
548
548
if ($ local ->isManyToOne ()) {
549
549
$ this ->getDocument ()->addLog (' Relation considered as "1 <=> N" ' );
550
550
551
+ $ variableName = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , true , $ local ));
552
+
551
553
$ writer
552
554
->write ('/** ' )
553
555
->writeIf ($ cacheMode , ' * ' .$ this ->getAnnotation ('Cache ' , array ($ cacheMode )))
@@ -561,7 +563,7 @@ protected function writeRelationsVar(WriterInterface $writer)
561
563
}
562
564
})
563
565
->write (' */ ' )
564
- ->write ('protected $ ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ targetEntity , $ related , true , $ local )) .'; ' )
566
+ ->write ('protected $ ' .$ variableName .'; ' )
565
567
->write ('' )
566
568
;
567
569
} else {
@@ -603,13 +605,15 @@ protected function writeRelationsVar(WriterInterface $writer)
603
605
if ($ foreign ->isManyToOne ()) {
604
606
$ this ->getDocument ()->addLog (' Relation considered as "N <=> 1" ' );
605
607
608
+ $ variableName = $ this ->getNaming ($ this ->getRelatedVarName ($ foreign ->getReferencedTable ()->getModelName (), $ related , false , $ foreign ));
609
+
606
610
$ writer
607
611
->write ('/** ' )
608
612
->writeIf ($ cacheMode , ' * ' .$ this ->getAnnotation ('Cache ' , array ($ cacheMode )))
609
613
->write (' * ' .$ this ->getAnnotation ('ManyToOne ' , $ annotationOptions ))
610
614
->write (' * ' .$ this ->getJoins ($ foreign , false ))
611
615
->write (' */ ' )
612
- ->write ('protected $ ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ targetEntity , $ related , false , $ foreign )) .'; ' )
616
+ ->write ('protected $ ' .$ variableName .'; ' )
613
617
->write ('' )
614
618
;
615
619
} else {
@@ -641,11 +645,13 @@ protected function writeManyToManyVar(WriterInterface $writer)
641
645
$ this ->getDocument ()->addLog (sprintf (' Writing setter/getter for N <=> N "%s" ' , $ relation ['refTable ' ]->getModelName ()));
642
646
643
647
$ fk1 = $ relation ['reference ' ];
648
+ $ fk2 = null ;
649
+
644
650
$ isOwningSide = $ this ->getFormatter ()->isOwningSide ($ relation , $ fk2 );
645
651
$ annotationOptions = array (
646
652
'targetEntity ' => $ relation ['refTable ' ]->getModelNameAsFQCN (),
647
653
'mappedBy ' => null ,
648
- 'inversedBy ' => $ this ->getNaming ($ this ->getPluralName ( )),
654
+ 'inversedBy ' => $ this ->getNaming ($ this ->getRelatedVarName ( $ this -> getModelName (), null , true , $ fk1 )),
649
655
'cascade ' => $ this ->getFormatter ()->getCascadeOption ($ fk1 ->parseComment ('cascade ' )),
650
656
'fetch ' => $ this ->getFormatter ()->getFetchOption ($ fk1 ->parseComment ('fetch ' )),
651
657
);
@@ -695,8 +701,10 @@ protected function writeManyToManyVar(WriterInterface $writer)
695
701
->write (' */ ' )
696
702
;
697
703
}
704
+
705
+ $ variableName = $ this ->getNaming ($ this ->getRelatedVarName ($ relation ['refTable ' ]->getModelName (), null , true , $ fk1 ));
698
706
$ writer
699
- ->write ('protected $ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getPluralName ()) .'; ' )
707
+ ->write ('protected $ ' .$ variableName .'; ' )
700
708
->write ('' )
701
709
;
702
710
}
@@ -741,15 +749,22 @@ public function writeRelationsConstructor(WriterInterface $writer)
741
749
$ this ->getDocument ()->addLog (sprintf (' Writing N <=> 1 constructor "%s" ' , $ local ->getOwningTable ()->getModelName ()));
742
750
743
751
$ related = $ local ->getForeignM2MRelatedName ();
744
- $ writer ->write ('$this->%s = new %s(); ' , $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getName (), $ related , true , $ local )), $ this ->getCollectionClass (false ));
752
+ $ variableName = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , true , $ local ));
753
+ $ writer ->write ('$this->%s = new %s(); ' , $ variableName , $ this ->getCollectionClass (false ));
745
754
}
746
755
}
747
756
748
757
public function writeManyToManyConstructor (WriterInterface $ writer )
749
758
{
750
759
foreach ($ this ->getTableM2MRelations () as $ relation ) {
760
+
761
+ $ fk1 = $ relation ['reference ' ];
762
+
751
763
$ this ->getDocument ()->addLog (sprintf (' Writing M2M constructor "%s" ' , $ relation ['refTable ' ]->getModelName ()));
752
- $ writer ->write ('$this->%s = new %s(); ' , $ this ->getNaming ($ relation ['refTable ' ]->getPluralName ()), $ this ->getCollectionClass (false ));
764
+ $ variableName = $ this ->getNaming ($ this ->getRelatedVarName ($ relation ['refTable ' ]->getModelName (), null , true , $ fk1 ));
765
+ $ writer ->write ('$this->%s = new %s(); ' , $ variableName , $ this ->getCollectionClass (false ));
766
+
767
+
753
768
}
754
769
}
755
770
@@ -785,8 +800,11 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
785
800
786
801
$ related = $ local ->getForeignM2MRelatedName ();
787
802
$ related_text = $ local ->getForeignM2MRelatedName (false );
788
- $ nameSingular = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , false , $ local , true ), Formatter::NAMING_PASCAL_CASE );
789
- $ namePlural = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , true , $ local ), Formatter::NAMING_PASCAL_CASE );
803
+ $ nameSingular = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , false , $ local , true ));
804
+ $ namePlural = $ this ->getNaming ($ this ->getRelatedVarName ($ local ->getOwningTable ()->getModelName (), $ related , true , $ local ));
805
+
806
+ $ variableNameSingular = $ nameSingular ;
807
+ $ variableNamePlural = $ namePlural ;
790
808
791
809
$ typehints = array (
792
810
'add_phpdoc_arg ' => $ this ->typehint ($ local ->getOwningTable ()->getNamespace (), false ),
@@ -808,14 +826,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
808
826
->write ('/** ' )
809
827
->write (' * Add ' .trim ($ local ->getOwningTable ()->getModelName ().' entity ' .$ related_text ). ' to collection (one to many). ' )
810
828
->write (' * ' )
811
- ->write (' * @param ' .$ typehints ['add_phpdoc_arg ' ].' $ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) )
829
+ ->write (' * @param ' .$ typehints ['add_phpdoc_arg ' ].' $ ' .$ variableNameSingular )
812
830
->write (' * ' )
813
831
->write (' * @return ' .$ typehints ['add_phpdoc_return ' ])
814
832
->write (' */ ' )
815
- ->write ('public function add ' .$ nameSingular .'( ' .$ typehints ['add_arg ' ].'$ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) .') ' .$ typehints ['add_return ' ])
833
+ ->write ('public function add ' .ucfirst ( $ nameSingular) .'( ' .$ typehints ['add_arg ' ].'$ ' .$ variableNameSingular .') ' .$ typehints ['add_return ' ])
816
834
->write ('{ ' )
817
835
->indent ()
818
- ->write ('$this-> ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ local -> getOwningTable ()-> getName (), $ related , true , $ local )) .'[] = $ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) .'; ' )
836
+ ->write ('$this-> ' .$ variableNamePlural .'[] = $ ' .$ variableNameSingular .'; ' )
819
837
->write ('' )
820
838
->write ('return $this; ' )
821
839
->outdent ()
@@ -825,14 +843,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
825
843
->write ('/** ' )
826
844
->write (' * Remove ' .trim ($ local ->getOwningTable ()->getModelName ().' entity ' .$ related_text ). ' from collection (one to many). ' )
827
845
->write (' * ' )
828
- ->write (' * @param ' .$ typehints ['remove_phpdoc_arg ' ].' $ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) )
846
+ ->write (' * @param ' .$ typehints ['remove_phpdoc_arg ' ].' $ ' .$ variableNameSingular )
829
847
->write (' * ' )
830
848
->write (' * @return ' .$ typehints ['remove_phpdoc_return ' ])
831
849
->write (' */ ' )
832
- ->write ('public function remove ' .$ nameSingular .'( ' .$ typehints ['remove_arg ' ].'$ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) .') ' .$ typehints ['remove_return ' ])
850
+ ->write ('public function remove ' .ucfirst ( $ nameSingular) .'( ' .$ typehints ['remove_arg ' ].'$ ' .$ variableNameSingular .') ' .$ typehints ['remove_return ' ])
833
851
->write ('{ ' )
834
852
->indent ()
835
- ->write ('$this-> ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ local -> getOwningTable ()-> getName (), $ related , true , $ local )) .'->removeElement($ ' .$ this -> getNaming ( $ local -> getOwningTable ()-> getName ()) .'); ' )
853
+ ->write ('$this-> ' .$ variableNamePlural .'->removeElement($ ' .$ variableNameSingular .'); ' )
836
854
->write ('' )
837
855
->write ('return $this; ' )
838
856
->outdent ()
@@ -844,10 +862,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
844
862
->write (' * ' )
845
863
->write (' * @return ' .$ typehints ['get_phpdoc ' ])
846
864
->write (' */ ' )
847
- ->write ('public function get ' .$ namePlural .'() ' .$ typehints ['get_return ' ])
865
+ ->write ('public function get ' .ucfirst ( $ namePlural) .'() ' .$ typehints ['get_return ' ])
848
866
->write ('{ ' )
849
867
->indent ()
850
- ->write ('return $this-> ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ local -> getOwningTable ()-> getName (), $ related , true , $ local )) .'; ' )
868
+ ->write ('return $this-> ' .$ variableNamePlural .'; ' )
851
869
->outdent ()
852
870
->write ('} ' )
853
871
->write ('' )
@@ -920,7 +938,8 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
920
938
921
939
$ related = $ this ->getRelatedName ($ foreign );
922
940
$ related_text = $ this ->getRelatedName ($ foreign , false );
923
- $ nameSingular = $ this ->getNaming ($ this ->getRelatedVarName ($ foreign ->getReferencedTable ()->getModelName (), $ related , false , $ foreign ), Formatter::NAMING_PASCAL_CASE );
941
+ $ nameSingular = $ this ->getNaming ($ this ->getRelatedVarName ($ foreign ->getReferencedTable ()->getModelName (), $ related , false , $ foreign ));
942
+ $ variableNameSingular = $ nameSingular ;
924
943
925
944
$ nullable = true ;
926
945
foreach ($ foreign ->getLocals () as $ lc ) {
@@ -942,14 +961,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
942
961
->write ('/** ' )
943
962
->write (' * Set ' .trim ($ foreign ->getReferencedTable ()->getModelName ().' entity ' .$ related_text ).' (many to one). ' )
944
963
->write (' * ' )
945
- ->write (' * @param ' .$ typehints ['set_phpdoc_arg ' ].' $ ' .$ this -> getNaming ( $ foreign -> getReferencedTable ()-> getName ()) )
964
+ ->write (' * @param ' .$ typehints ['set_phpdoc_arg ' ].' $ ' .$ variableNameSingular )
946
965
->write (' * ' )
947
966
->write (' * @return ' .$ typehints ['set_phpdoc_return ' ])
948
967
->write (' */ ' )
949
- ->write ('public function set ' .$ nameSingular .'( ' .$ typehints ['set_arg ' ].'$ ' .$ this -> getNaming ( $ foreign -> getReferencedTable ()-> getName ()) .') ' .$ typehints ['set_return ' ])
968
+ ->write ('public function set ' .ucfirst ( $ nameSingular) .'( ' .$ typehints ['set_arg ' ].'$ ' .$ variableNameSingular .') ' .$ typehints ['set_return ' ])
950
969
->write ('{ ' )
951
970
->indent ()
952
- ->write ('$this-> ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ foreign -> getReferencedTable ()-> getName (), $ related , false , $ foreign )) .' = $ ' .$ this -> getNaming ( $ foreign -> getReferencedTable ()-> getModelName ()) .'; ' )
971
+ ->write ('$this-> ' .$ variableNameSingular .' = $ ' .$ variableNameSingular .'; ' )
953
972
->write ('' )
954
973
->write ('return $this; ' )
955
974
->outdent ()
@@ -961,10 +980,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
961
980
->write (' * ' )
962
981
->write (' * @return ' .$ typehints ['get_phpdoc ' ])
963
982
->write (' */ ' )
964
- ->write ('public function get ' .$ nameSingular .'() ' .$ typehints ['get_return ' ])
983
+ ->write ('public function get ' .ucfirst ( $ nameSingular) .'() ' .$ typehints ['get_return ' ])
965
984
->write ('{ ' )
966
985
->indent ()
967
- ->write ('return $this-> ' .$ this -> getNaming ( $ this -> getRelatedVarName ( $ foreign -> getReferencedTable ()-> getName (), $ related , false , $ foreign )) .'; ' )
986
+ ->write ('return $this-> ' .$ variableNameSingular .'; ' )
968
987
->outdent ()
969
988
->write ('} ' )
970
989
->write ('' )
@@ -1030,6 +1049,8 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
1030
1049
foreach ($ this ->getTableM2MRelations () as $ relation ) {
1031
1050
$ this ->getDocument ()->addLog (sprintf (' Writing N <=> N relation "%s" ' , $ relation ['refTable ' ]->getModelName ()));
1032
1051
1052
+ $ fk2 = null ;
1053
+
1033
1054
$ isOwningSide = $ this ->getFormatter ()->isOwningSide ($ relation , $ fk2 );
1034
1055
1035
1056
$ typehints = array (
@@ -1047,23 +1068,26 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
1047
1068
'get_return ' => $ this ->returnTypehint (null , false ),
1048
1069
);
1049
1070
1071
+ $ variableNameSingular = $ this ->getNaming ($ this ->getRelatedVarName ($ relation ['refTable ' ]->getModelName (), null , false , $ fk2 ));
1072
+ $ variableNamePlural = $ this ->getNaming ($ this ->getRelatedVarName ($ relation ['refTable ' ]->getModelName (), null , true , $ fk2 ));
1073
+
1050
1074
$ writer
1051
1075
->write ('/** ' )
1052
1076
->write (' * Add ' .$ relation ['refTable ' ]->getModelName ().' entity to collection. ' )
1053
1077
->write (' * ' )
1054
- ->write (' * @param ' .$ typehints ['add_phpdoc_arg ' ].' $ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getName ()) )
1078
+ ->write (' * @param ' .$ typehints ['add_phpdoc_arg ' ].' $ ' .$ variableNameSingular )
1055
1079
->write (' * ' )
1056
1080
->write (' * @return ' .$ typehints ['add_phpdoc_return ' ])
1057
1081
->write (' */ ' )
1058
- ->write ('public function add ' .$ relation [ ' refTable ' ]-> getModelName ( ).'( ' .$ typehints ['add_arg ' ].'$ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getName ()) .') ' .$ typehints ['add_return ' ])
1082
+ ->write ('public function add ' .ucfirst ( $ variableNameSingular ).'( ' .$ typehints ['add_arg ' ].'$ ' .$ variableNameSingular .') ' .$ typehints ['add_return ' ])
1059
1083
->write ('{ ' )
1060
1084
->indent ()
1061
- ->writeCallback (function (WriterInterface $ writer , Table $ _this = null ) use ($ isOwningSide , $ relation ) {
1085
+ ->writeCallback (function (WriterInterface $ writer , Table $ _this = null ) use ($ isOwningSide , $ relation, $ variableNameSingular , $ fk2 ) {
1062
1086
if ($ isOwningSide ) {
1063
- $ writer ->write ('$%s->add%s($this); ' , $ _this ->getNaming ($ relation [ ' refTable ' ]-> getName ()), $ _this ->getModelName ());
1087
+ $ writer ->write ('$%s->add%s($this); ' , $ variableNameSingular , ucfirst ( $ this ->getNaming ($ this -> getRelatedVarName ( $ _this ->getModelName (), null , false , $ fk2 )) ));
1064
1088
}
1065
1089
})
1066
- ->write ('$this-> ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getPluralName ()) .'[] = $ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getName ()) .'; ' )
1090
+ ->write ('$this-> ' .$ variableNamePlural .'[] = $ ' .$ variableNameSingular .'; ' )
1067
1091
->write ('' )
1068
1092
->write ('return $this; ' )
1069
1093
->outdent ()
@@ -1072,19 +1096,19 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
1072
1096
->write ('/** ' )
1073
1097
->write (' * Remove ' .$ relation ['refTable ' ]->getModelName ().' entity from collection. ' )
1074
1098
->write (' * ' )
1075
- ->write (' * @param ' .$ typehints ['remove_phpdoc_arg ' ].' $ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getName ()) )
1099
+ ->write (' * @param ' .$ typehints ['remove_phpdoc_arg ' ].' $ ' .$ variableNameSingular )
1076
1100
->write (' * ' )
1077
1101
->write (' * @return ' .$ typehints ['remove_phpdoc_return ' ])
1078
1102
->write (' */ ' )
1079
- ->write ('public function remove ' .$ relation [ ' refTable ' ]-> getModelName ( ).'( ' .$ typehints ['remove_arg ' ].'$ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getName ()) .') ' .$ typehints ['remove_return ' ])
1103
+ ->write ('public function remove ' .ucfirst ( $ variableNameSingular ).'( ' .$ typehints ['remove_arg ' ].'$ ' .$ variableNameSingular .') ' .$ typehints ['remove_return ' ])
1080
1104
->write ('{ ' )
1081
1105
->indent ()
1082
- ->writeCallback (function (WriterInterface $ writer , Table $ _this = null ) use ($ isOwningSide , $ relation ) {
1106
+ ->writeCallback (function (WriterInterface $ writer , Table $ _this = null ) use ($ isOwningSide , $ relation, $ variableNameSingular , $ fk2 ) {
1083
1107
if ($ isOwningSide ) {
1084
- $ writer ->write ('$%s->remove%s($this); ' , $ _this ->getNaming ($ relation [ ' refTable ' ]-> getName ()), $ _this ->getModelName ());
1108
+ $ writer ->write ('$%s->remove%s($this); ' , $ variableNameSingular , ucfirst ( $ this ->getNaming ($ this -> getRelatedVarName ( $ _this ->getModelName (), null , false , $ fk2 )) ));
1085
1109
}
1086
1110
})
1087
- ->write ('$this-> ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getPluralName ()) .'->removeElement($ ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getModelName ()) .'); ' )
1111
+ ->write ('$this-> ' .$ variableNamePlural .'->removeElement($ ' .$ variableNameSingular .'); ' )
1088
1112
->write ('' )
1089
1113
->write ('return $this; ' )
1090
1114
->outdent ()
@@ -1095,10 +1119,10 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
1095
1119
->write (' * ' )
1096
1120
->write (' * @return ' .$ typehints ['get_phpdoc ' ])
1097
1121
->write (' */ ' )
1098
- ->write ('public function get ' .$ relation [ ' refTable ' ]-> getPluralModelName ( ).'() ' .$ typehints ['get_return ' ])
1122
+ ->write ('public function get ' .ucfirst ( $ variableNamePlural ).'() ' .$ typehints ['get_return ' ])
1099
1123
->write ('{ ' )
1100
1124
->indent ()
1101
- ->write ('return $this-> ' .$ this -> getNaming ( $ relation [ ' refTable ' ]-> getPluralName ()) .'; ' )
1125
+ ->write ('return $this-> ' .$ variableNamePlural .'; ' )
1102
1126
->outdent ()
1103
1127
->write ('} ' )
1104
1128
->write ('' )
0 commit comments