Skip to content

Commit

Permalink
refactor relatedNames tag to work with M2M relations (on the m2m ta…
Browse files Browse the repository at this point in the history
…ble), fix compatibility issues with latest updates
  • Loading branch information
adrianbardan committed Aug 9, 2020
1 parent 8f997ff commit 63f05dc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 35 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ Common Setup Options for Doctrine 2.0:

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".

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.
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:

{d:relatedNames}
StoreProduct:Product
StoreCategory:Category
{/d:relatedNames}

This will generate `products` and `categories` collection properties (along with methods for adding and removing items) on each of the two entities.

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

Expand Down
92 changes: 58 additions & 34 deletions lib/Annotation/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ protected function writeRelationsVar(WriterInterface $writer)
if ($local->isManyToOne()) {
$this->getDocument()->addLog(' Relation considered as "1 <=> N"');

$variableName = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, true, $local));

$writer
->write('/**')
->writeIf($cacheMode, ' * '.$this->getAnnotation('Cache', array($cacheMode)))
Expand All @@ -561,7 +563,7 @@ protected function writeRelationsVar(WriterInterface $writer)
}
})
->write(' */')
->write('protected $'.$this->getNaming($this->getRelatedVarName($targetEntity, $related, true, $local)).';')
->write('protected $'.$variableName.';')
->write('')
;
} else {
Expand Down Expand Up @@ -603,13 +605,15 @@ protected function writeRelationsVar(WriterInterface $writer)
if ($foreign->isManyToOne()) {
$this->getDocument()->addLog(' Relation considered as "N <=> 1"');

$variableName = $this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getModelName(), $related, false, $foreign));

$writer
->write('/**')
->writeIf($cacheMode, ' * '.$this->getAnnotation('Cache', array($cacheMode)))
->write(' * '.$this->getAnnotation('ManyToOne', $annotationOptions))
->write(' * '.$this->getJoins($foreign, false))
->write(' */')
->write('protected $'.$this->getNaming($this->getRelatedVarName($targetEntity, $related, false, $foreign)).';')
->write('protected $'.$variableName.';')
->write('')
;
} else {
Expand Down Expand Up @@ -641,11 +645,13 @@ protected function writeManyToManyVar(WriterInterface $writer)
$this->getDocument()->addLog(sprintf(' Writing setter/getter for N <=> N "%s"', $relation['refTable']->getModelName()));

$fk1 = $relation['reference'];
$fk2 = null;

$isOwningSide = $this->getFormatter()->isOwningSide($relation, $fk2);
$annotationOptions = array(
'targetEntity' => $relation['refTable']->getModelNameAsFQCN(),
'mappedBy' => null,
'inversedBy' => $this->getNaming($this->getPluralName()),
'inversedBy' => $this->getNaming($this->getRelatedVarName($this->getModelName(), null, true, $fk1)),
'cascade' => $this->getFormatter()->getCascadeOption($fk1->parseComment('cascade')),
'fetch' => $this->getFormatter()->getFetchOption($fk1->parseComment('fetch')),
);
Expand Down Expand Up @@ -695,8 +701,10 @@ protected function writeManyToManyVar(WriterInterface $writer)
->write(' */')
;
}

$variableName = $this->getNaming($this->getRelatedVarName($relation['refTable']->getModelName(), null, true, $fk1));
$writer
->write('protected $'.$this->getNaming($relation['refTable']->getPluralName()).';')
->write('protected $'.$variableName.';')
->write('')
;
}
Expand Down Expand Up @@ -741,15 +749,22 @@ public function writeRelationsConstructor(WriterInterface $writer)
$this->getDocument()->addLog(sprintf(' Writing N <=> 1 constructor "%s"', $local->getOwningTable()->getModelName()));

$related = $local->getForeignM2MRelatedName();
$writer->write('$this->%s = new %s();', $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)), $this->getCollectionClass(false));
$variableName = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, true, $local));
$writer->write('$this->%s = new %s();', $variableName, $this->getCollectionClass(false));
}
}

public function writeManyToManyConstructor(WriterInterface $writer)
{
foreach ($this->getTableM2MRelations() as $relation) {

$fk1 = $relation['reference'];

$this->getDocument()->addLog(sprintf(' Writing M2M constructor "%s"', $relation['refTable']->getModelName()));
$writer->write('$this->%s = new %s();', $this->getNaming($relation['refTable']->getPluralName()), $this->getCollectionClass(false));
$variableName = $this->getNaming($this->getRelatedVarName($relation['refTable']->getModelName(), null, true, $fk1));
$writer->write('$this->%s = new %s();', $variableName, $this->getCollectionClass(false));


}
}

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

$related = $local->getForeignM2MRelatedName();
$related_text = $local->getForeignM2MRelatedName(false);
$nameSingular = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, false, $local, true), Formatter::NAMING_PASCAL_CASE);
$namePlural = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, true, $local), Formatter::NAMING_PASCAL_CASE);
$nameSingular = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, false, $local, true));
$namePlural = $this->getNaming($this->getRelatedVarName($local->getOwningTable()->getModelName(), $related, true, $local));

$variableNameSingular = $nameSingular;
$variableNamePlural = $namePlural;

$typehints = array(
'add_phpdoc_arg' => $this->typehint($local->getOwningTable()->getNamespace(), false),
Expand All @@ -808,14 +826,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
->write('/**')
->write(' * Add '.trim($local->getOwningTable()->getModelName().' entity '.$related_text). ' to collection (one to many).')
->write(' *')
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$this->getNaming($local->getOwningTable()->getName()))
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$variableNameSingular)
->write(' *')
->write(' * @return '.$typehints['add_phpdoc_return'])
->write(' */')
->write('public function add'.$nameSingular.'('.$typehints['add_arg'].'$'.$this->getNaming($local->getOwningTable()->getName()).')'.$typehints['add_return'])
->write('public function add'.ucfirst($nameSingular).'('.$typehints['add_arg'].'$'.$variableNameSingular.')'.$typehints['add_return'])
->write('{')
->indent()
->write('$this->'.$this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)).'[] = $'.$this->getNaming($local->getOwningTable()->getName()).';')
->write('$this->'.$variableNamePlural.'[] = $'.$variableNameSingular.';')
->write('')
->write('return $this;')
->outdent()
Expand All @@ -825,14 +843,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
->write('/**')
->write(' * Remove '.trim($local->getOwningTable()->getModelName().' entity '.$related_text). ' from collection (one to many).')
->write(' *')
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$this->getNaming($local->getOwningTable()->getName()))
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$variableNameSingular)
->write(' *')
->write(' * @return '.$typehints['remove_phpdoc_return'])
->write(' */')
->write('public function remove'.$nameSingular.'('.$typehints['remove_arg'].'$'.$this->getNaming($local->getOwningTable()->getName()).')'.$typehints['remove_return'])
->write('public function remove'.ucfirst($nameSingular).'('.$typehints['remove_arg'].'$'.$variableNameSingular.')'.$typehints['remove_return'])
->write('{')
->indent()
->write('$this->'.$this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)).'->removeElement($'.$this->getNaming($local->getOwningTable()->getName()).');')
->write('$this->'.$variableNamePlural.'->removeElement($'.$variableNameSingular.');')
->write('')
->write('return $this;')
->outdent()
Expand All @@ -844,10 +862,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
->write(' *')
->write(' * @return '.$typehints['get_phpdoc'])
->write(' */')
->write('public function get'.$namePlural.'()'.$typehints['get_return'])
->write('public function get'.ucfirst($namePlural).'()'.$typehints['get_return'])
->write('{')
->indent()
->write('return $this->'.$this->getNaming($this->getRelatedVarName($local->getOwningTable()->getName(), $related, true, $local)).';')
->write('return $this->'.$variableNamePlural.';')
->outdent()
->write('}')
->write('')
Expand Down Expand Up @@ -920,7 +938,8 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)

$related = $this->getRelatedName($foreign);
$related_text = $this->getRelatedName($foreign, false);
$nameSingular = $this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getModelName(), $related, false, $foreign), Formatter::NAMING_PASCAL_CASE);
$nameSingular = $this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getModelName(), $related, false, $foreign));
$variableNameSingular = $nameSingular;

$nullable = true;
foreach ($foreign->getLocals() as $lc) {
Expand All @@ -942,14 +961,14 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
->write('/**')
->write(' * Set '.trim($foreign->getReferencedTable()->getModelName().' entity '.$related_text).' (many to one).')
->write(' *')
->write(' * @param '.$typehints['set_phpdoc_arg'].' $'.$this->getNaming($foreign->getReferencedTable()->getName()))
->write(' * @param '.$typehints['set_phpdoc_arg'].' $'.$variableNameSingular)
->write(' *')
->write(' * @return '.$typehints['set_phpdoc_return'])
->write(' */')
->write('public function set'.$nameSingular.'('.$typehints['set_arg'].'$'.$this->getNaming($foreign->getReferencedTable()->getName()).')'.$typehints['set_return'])
->write('public function set'.ucfirst($nameSingular).'('.$typehints['set_arg'].'$'.$variableNameSingular.')'.$typehints['set_return'])
->write('{')
->indent()
->write('$this->'.$this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getName(), $related, false, $foreign)).' = $'.$this->getNaming($foreign->getReferencedTable()->getModelName()).';')
->write('$this->'.$variableNameSingular.' = $'.$variableNameSingular.';')
->write('')
->write('return $this;')
->outdent()
Expand All @@ -961,10 +980,10 @@ protected function writeRelationsGetterAndSetter(WriterInterface $writer)
->write(' *')
->write(' * @return '.$typehints['get_phpdoc'])
->write(' */')
->write('public function get'.$nameSingular.'()'.$typehints['get_return'])
->write('public function get'.ucfirst($nameSingular).'()'.$typehints['get_return'])
->write('{')
->indent()
->write('return $this->'.$this->getNaming($this->getRelatedVarName($foreign->getReferencedTable()->getName(), $related, false, $foreign)).';')
->write('return $this->'.$variableNameSingular.';')
->outdent()
->write('}')
->write('')
Expand Down Expand Up @@ -1030,6 +1049,8 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
foreach ($this->getTableM2MRelations() as $relation) {
$this->getDocument()->addLog(sprintf(' Writing N <=> N relation "%s"', $relation['refTable']->getModelName()));

$fk2 = null;

$isOwningSide = $this->getFormatter()->isOwningSide($relation, $fk2);

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

$variableNameSingular = $this->getNaming($this->getRelatedVarName($relation['refTable']->getModelName(), null, false, $fk2));
$variableNamePlural = $this->getNaming($this->getRelatedVarName($relation['refTable']->getModelName(), null, true, $fk2));

$writer
->write('/**')
->write(' * Add '.$relation['refTable']->getModelName().' entity to collection.')
->write(' *')
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$this->getNaming($relation['refTable']->getName()))
->write(' * @param '.$typehints['add_phpdoc_arg'].' $'.$variableNameSingular)
->write(' *')
->write(' * @return '.$typehints['add_phpdoc_return'])
->write(' */')
->write('public function add'.$relation['refTable']->getModelName().'('.$typehints['add_arg'].'$'.$this->getNaming($relation['refTable']->getName()).')'.$typehints['add_return'])
->write('public function add'.ucfirst($variableNameSingular).'('.$typehints['add_arg'].'$'.$variableNameSingular.')'.$typehints['add_return'])
->write('{')
->indent()
->writeCallback(function(WriterInterface $writer, Table $_this = null) use ($isOwningSide, $relation) {
->writeCallback(function(WriterInterface $writer, Table $_this = null) use ($isOwningSide, $relation, $variableNameSingular, $fk2) {
if ($isOwningSide) {
$writer->write('$%s->add%s($this);', $_this->getNaming($relation['refTable']->getName()), $_this->getModelName());
$writer->write('$%s->add%s($this);', $variableNameSingular, ucfirst($this->getNaming($this->getRelatedVarName($_this->getModelName(), null, false, $fk2))));
}
})
->write('$this->'.$this->getNaming($relation['refTable']->getPluralName()).'[] = $'.$this->getNaming($relation['refTable']->getName()).';')
->write('$this->'.$variableNamePlural.'[] = $'.$variableNameSingular.';')
->write('')
->write('return $this;')
->outdent()
Expand All @@ -1072,19 +1096,19 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
->write('/**')
->write(' * Remove '.$relation['refTable']->getModelName().' entity from collection.')
->write(' *')
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$this->getNaming($relation['refTable']->getName()))
->write(' * @param '.$typehints['remove_phpdoc_arg'].' $'.$variableNameSingular)
->write(' *')
->write(' * @return '.$typehints['remove_phpdoc_return'])
->write(' */')
->write('public function remove'.$relation['refTable']->getModelName().'('.$typehints['remove_arg'].'$'.$this->getNaming($relation['refTable']->getName()).')'.$typehints['remove_return'])
->write('public function remove'.ucfirst($variableNameSingular).'('.$typehints['remove_arg'].'$'.$variableNameSingular.')'.$typehints['remove_return'])
->write('{')
->indent()
->writeCallback(function(WriterInterface $writer, Table $_this = null) use ($isOwningSide, $relation) {
->writeCallback(function(WriterInterface $writer, Table $_this = null) use ($isOwningSide, $relation, $variableNameSingular, $fk2) {
if ($isOwningSide) {
$writer->write('$%s->remove%s($this);', $_this->getNaming($relation['refTable']->getName()), $_this->getModelName());
$writer->write('$%s->remove%s($this);', $variableNameSingular, ucfirst($this->getNaming($this->getRelatedVarName($_this->getModelName(), null, false, $fk2))));
}
})
->write('$this->'.$this->getNaming($relation['refTable']->getPluralName()).'->removeElement($'.$this->getNaming($relation['refTable']->getModelName()).');')
->write('$this->'.$variableNamePlural.'->removeElement($'.$variableNameSingular.');')
->write('')
->write('return $this;')
->outdent()
Expand All @@ -1095,10 +1119,10 @@ protected function writeManyToManyGetterAndSetter(WriterInterface $writer)
->write(' *')
->write(' * @return '.$typehints['get_phpdoc'])
->write(' */')
->write('public function get'.$relation['refTable']->getPluralModelName().'()'.$typehints['get_return'])
->write('public function get'.ucfirst($variableNamePlural).'()'.$typehints['get_return'])
->write('{')
->indent()
->write('return $this->'.$this->getNaming($relation['refTable']->getPluralName()).';')
->write('return $this->'.$variableNamePlural.';')
->outdent()
->write('}')
->write('')
Expand Down
Loading

0 comments on commit 63f05dc

Please sign in to comment.