Skip to content

Commit 63f05dc

Browse files
committed
refactor relatedNames tag to work with M2M relations (on the m2m table), fix compatibility issues with latest updates
1 parent 8f997ff commit 63f05dc

File tree

3 files changed

+92
-35
lines changed

3 files changed

+92
-35
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ Common Setup Options for Doctrine 2.0:
242242

243243
The generated StoreProduct class will have "category" and "image" properties instead of "storeProductCategory" and "storeProductImage", while the "StoreProductImage" class will have a "product" property instead of "storeProduct".
244244

245+
This tag can also be used on Many to Many tables (in comments as well), to change the relation names used in each of the 2 tables.
246+
For example, on a store_product_category m2m table, having two foreign keys only (store_product_id and store_category_id), we could have the following:
247+
248+
{d:relatedNames}
249+
StoreProduct:Product
250+
StoreCategory:Category
251+
{/d:relatedNames}
252+
253+
This will generate `products` and `categories` collection properties (along with methods for adding and removing items) on each of the two entities.
245254

246255
* `{d:relationNames}oneToManyName:manyToOneName{/d:relationNames}` (applied to ForeignKey)
247256

lib/Annotation/Model/Table.php

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ protected function writeRelationsVar(WriterInterface $writer)
548548
if ($local->isManyToOne()) {
549549
$this->getDocument()->addLog(' Relation considered as "1 <=> N"');
550550

551+
$variableName = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, true, $local));
552+
551553
$writer
552554
->write('/**')
553555
->writeIf($cacheMode, ' * '.$this->getAnnotation('Cache', array($cacheMode)))
@@ -561,7 +563,7 @@ protected function writeRelationsVar(WriterInterface $writer)
561563
}
562564
})
563565
->write(' */')
564-
->write('protected $'.$this->getNaming($this->getRelatedVarName($targetEntity, $related, true, $local)).';')
566+
->write('protected $'.$variableName.';')
565567
->write('')
566568
;
567569
} else {
@@ -603,13 +605,15 @@ protected function writeRelationsVar(WriterInterface $writer)
603605
if ($foreign->isManyToOne()) {
604606
$this->getDocument()->addLog(' Relation considered as "N <=> 1"');
605607

608+
$variableName = $this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getModelName(), $related, false, $foreign));
609+
606610
$writer
607611
->write('/**')
608612
->writeIf($cacheMode, ' * '.$this->getAnnotation('Cache', array($cacheMode)))
609613
->write(' * '.$this->getAnnotation('ManyToOne', $annotationOptions))
610614
->write(' * '.$this->getJoins($foreign, false))
611615
->write(' */')
612-
->write('protected $'.$this->getNaming($this->getRelatedVarName($targetEntity, $related, false, $foreign)).';')
616+
->write('protected $'.$variableName.';')
613617
->write('')
614618
;
615619
} else {
@@ -641,11 +645,13 @@ protected function writeManyToManyVar(WriterInterface $writer)
641645
$this->getDocument()->addLog(sprintf(' Writing setter/getter for N <=> N "%s"', $relation['refTable']->getModelName()));
642646

643647
$fk1 = $relation['reference'];
648+
$fk2 = null;
649+
644650
$isOwningSide = $this->getFormatter()->isOwningSide($relation, $fk2);
645651
$annotationOptions = array(
646652
'targetEntity' => $relation['refTable']->getModelNameAsFQCN(),
647653
'mappedBy' => null,
648-
'inversedBy' => $this->getNaming($this->getPluralName()),
654+
'inversedBy' => $this->getNaming($this->getRelatedVarName($this->getModelName(), null, true, $fk1)),
649655
'cascade' => $this->getFormatter()->getCascadeOption($fk1->parseComment('cascade')),
650656
'fetch' => $this->getFormatter()->getFetchOption($fk1->parseComment('fetch')),
651657
);
@@ -695,8 +701,10 @@ protected function writeManyToManyVar(WriterInterface $writer)
695701
->write(' */')
696702
;
697703
}
704+
705+
$variableName = $this->getNaming($this->getRelatedVarName($relation['refTable']->getModelName(), null, true, $fk1));
698706
$writer
699-
->write('protected $'.$this->getNaming($relation['refTable']->getPluralName()).';')
707+
->write('protected $'.$variableName.';')
700708
->write('')
701709
;
702710
}
@@ -741,15 +749,22 @@ public function writeRelationsConstructor(WriterInterface $writer)
741749
$this->getDocument()->addLog(sprintf(' Writing N <=> 1 constructor "%s"', $local->getOwningTable()->getModelName()));
742750

743751
$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));
745754
}
746755
}
747756

748757
public function writeManyToManyConstructor(WriterInterface $writer)
749758
{
750759
foreach ($this->getTableM2MRelations() as $relation) {
760+
761+
$fk1 = $relation['reference'];
762+
751763
$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+
753768
}
754769
}
755770

@@ -785,8 +800,11 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
785800

786801
$related = $local->getForeignM2MRelatedName();
787802
$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;
790808

791809
$typehints = array(
792810
'add_phpdoc_arg' => $this->typehint($local->getOwningTable()->getNamespace(), false),
@@ -808,14 +826,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
808826
->write('/**')
809827
->write(' * Add '.trim($local->getOwningTable()->getModelName().' entity '.$related_text). ' to collection (one to many).')
810828
->write(' *')
811-
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$this->getNaming($local->getOwningTable()->getName()))
829+
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$variableNameSingular)
812830
->write(' *')
813831
->write(' * @return '.$typehints['add_phpdoc_return'])
814832
->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'])
816834
->write('{')
817835
->indent()
818-
->write('$this->'.$this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)).'[] = $'.$this->getNaming($local->getOwningTable()->getName()).';')
836+
->write('$this->'.$variableNamePlural.'[] = $'.$variableNameSingular.';')
819837
->write('')
820838
->write('return $this;')
821839
->outdent()
@@ -825,14 +843,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
825843
->write('/**')
826844
->write(' * Remove '.trim($local->getOwningTable()->getModelName().' entity '.$related_text). ' from collection (one to many).')
827845
->write(' *')
828-
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$this->getNaming($local->getOwningTable()->getName()))
846+
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$variableNameSingular)
829847
->write(' *')
830848
->write(' * @return '.$typehints['remove_phpdoc_return'])
831849
->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'])
833851
->write('{')
834852
->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.');')
836854
->write('')
837855
->write('return $this;')
838856
->outdent()
@@ -844,10 +862,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
844862
->write(' *')
845863
->write(' * @return '.$typehints['get_phpdoc'])
846864
->write(' */')
847-
->write('public function get'.$namePlural.'()'.$typehints['get_return'])
865+
->write('public function get'.ucfirst($namePlural).'()'.$typehints['get_return'])
848866
->write('{')
849867
->indent()
850-
->write('return $this->'.$this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)).';')
868+
->write('return $this->'.$variableNamePlural.';')
851869
->outdent()
852870
->write('}')
853871
->write('')
@@ -920,7 +938,8 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
920938

921939
$related = $this->getRelatedName($foreign);
922940
$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;
924943

925944
$nullable = true;
926945
foreach ($foreign->getLocals() as $lc) {
@@ -942,14 +961,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
942961
->write('/**')
943962
->write(' * Set '.trim($foreign->getReferencedTable()->getModelName().' entity '.$related_text).' (many to one).')
944963
->write(' *')
945-
->write(' * @param '.$typehints['set_phpdoc_arg'].' $'.$this->getNaming($foreign->getReferencedTable()->getName()))
964+
->write(' * @param '.$typehints['set_phpdoc_arg'].' $'.$variableNameSingular)
946965
->write(' *')
947966
->write(' * @return '.$typehints['set_phpdoc_return'])
948967
->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'])
950969
->write('{')
951970
->indent()
952-
->write('$this->'.$this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getName(), $related, false, $foreign)).' = $'.$this->getNaming($foreign->getReferencedTable()->getModelName()).';')
971+
->write('$this->'.$variableNameSingular.' = $'.$variableNameSingular.';')
953972
->write('')
954973
->write('return $this;')
955974
->outdent()
@@ -961,10 +980,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
961980
->write(' *')
962981
->write(' * @return '.$typehints['get_phpdoc'])
963982
->write(' */')
964-
->write('public function get'.$nameSingular.'()'.$typehints['get_return'])
983+
->write('public function get'.ucfirst($nameSingular).'()'.$typehints['get_return'])
965984
->write('{')
966985
->indent()
967-
->write('return $this->'.$this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getName(), $related, false, $foreign)).';')
986+
->write('return $this->'.$variableNameSingular.';')
968987
->outdent()
969988
->write('}')
970989
->write('')
@@ -1030,6 +1049,8 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
10301049
foreach ($this->getTableM2MRelations() as $relation) {
10311050
$this->getDocument()->addLog(sprintf(' Writing N <=> N relation "%s"', $relation['refTable']->getModelName()));
10321051

1052+
$fk2 = null;
1053+
10331054
$isOwningSide = $this->getFormatter()->isOwningSide($relation, $fk2);
10341055

10351056
$typehints = array(
@@ -1047,23 +1068,26 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
10471068
'get_return' => $this->returnTypehint(null, false),
10481069
);
10491070

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+
10501074
$writer
10511075
->write('/**')
10521076
->write(' * Add '.$relation['refTable']->getModelName().' entity to collection.')
10531077
->write(' *')
1054-
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$this->getNaming($relation['refTable']->getName()))
1078+
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$variableNameSingular)
10551079
->write(' *')
10561080
->write(' * @return '.$typehints['add_phpdoc_return'])
10571081
->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'])
10591083
->write('{')
10601084
->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) {
10621086
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))));
10641088
}
10651089
})
1066-
->write('$this->'.$this->getNaming($relation['refTable']->getPluralName()).'[] = $'.$this->getNaming($relation['refTable']->getName()).';')
1090+
->write('$this->'.$variableNamePlural.'[] = $'.$variableNameSingular.';')
10671091
->write('')
10681092
->write('return $this;')
10691093
->outdent()
@@ -1072,19 +1096,19 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
10721096
->write('/**')
10731097
->write(' * Remove '.$relation['refTable']->getModelName().' entity from collection.')
10741098
->write(' *')
1075-
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$this->getNaming($relation['refTable']->getName()))
1099+
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$variableNameSingular)
10761100
->write(' *')
10771101
->write(' * @return '.$typehints['remove_phpdoc_return'])
10781102
->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'])
10801104
->write('{')
10811105
->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) {
10831107
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))));
10851109
}
10861110
})
1087-
->write('$this->'.$this->getNaming($relation['refTable']->getPluralName()).'->removeElement($'.$this->getNaming($relation['refTable']->getModelName()).');')
1111+
->write('$this->'.$variableNamePlural.'->removeElement($'.$variableNameSingular.');')
10881112
->write('')
10891113
->write('return $this;')
10901114
->outdent()
@@ -1095,10 +1119,10 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
10951119
->write(' *')
10961120
->write(' * @return '.$typehints['get_phpdoc'])
10971121
->write(' */')
1098-
->write('public function get'.$relation['refTable']->getPluralModelName().'()'.$typehints['get_return'])
1122+
->write('public function get'.ucfirst($variableNamePlural).'()'.$typehints['get_return'])
10991123
->write('{')
11001124
->indent()
1101-
->write('return $this->'.$this->getNaming($relation['refTable']->getPluralName()).';')
1125+
->write('return $this->'.$variableNamePlural.';')
11021126
->outdent()
11031127
->write('}')
11041128
->write('')

0 commit comments

Comments
 (0)