Skip to content

Commit d3cfe52

Browse files
committed
Merge branch 'autoPivot' of github.com:Kharhamel/tdbm into autoPivot2
2 parents 7b8ef9d + 871d90a commit d3cfe52

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/Utils/DefaultNamingStrategy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,9 @@ private function getSchema(): Schema
326326
}
327327
return $this->schema;
328328
}
329+
330+
public function getAutopivotEntityNameFrom(ForeignKeyConstraint $constraint, bool $useAlternativeName): string
331+
{
332+
return $this->getForeignKeyUpperCamelCaseName($constraint, $useAlternativeName);
333+
}
329334
}

src/Utils/NamingStrategyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ public function getJsonProperty(AbstractBeanPropertyDescriptor $property): strin
9292
* @return string
9393
*/
9494
public function getFindByIndexMethodName(Index $index, array $elements): string;
95+
96+
public function getAutopivotEntityNameFrom(ForeignKeyConstraint $constraint, bool $useAlternativeName): string;
9597
}

src/Utils/PivotTableMethodsDescriptor.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace TheCodingMachine\TDBM\Utils;
55

6+
use Doctrine\Common\Inflector\Inflector;
67
use Doctrine\DBAL\Schema\Column;
78
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
89
use Doctrine\DBAL\Schema\Table;
@@ -114,7 +115,10 @@ public function getBeanClassName(): string
114115
private function getPluralName() : string
115116
{
116117
if ($this->isAutoPivot()) {
117-
$name = $this->remoteFk->getForeignTableName().'By_'.$this->pivotTable->getName().'Via_'.implode('And_', $this->localFk->getUnquotedLocalColumns());
118+
$name = Inflector::pluralize($this->namingStrategy->getAutopivotEntityNameFrom($this->remoteFk, false));
119+
if ($this->useAlternateName) {
120+
$name .= 'By_'.$this->pivotTable->getName();
121+
}
118122
} elseif (!$this->useAlternateName) {
119123
$name = $this->remoteFk->getForeignTableName();
120124
} else {
@@ -131,8 +135,11 @@ private function getPluralName() : string
131135
private function getSingularName() : string
132136
{
133137
if ($this->isAutoPivot()) {
134-
$name = TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()).'By_'.$this->pivotTable->getName().'Via_'.implode('And_', $this->localFk->getUnquotedLocalColumns());
135-
} elseif (!$this->useAlternateName) {
138+
$name = $this->namingStrategy->getAutopivotEntityNameFrom($this->remoteFk, false);
139+
if ($this->useAlternateName) {
140+
$name .= 'By_'.$this->pivotTable->getName();
141+
}
142+
} else if (!$this->useAlternateName) {
136143
$name = TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName());
137144
} else {
138145
$name = TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()).'By_'.$this->pivotTable->getName();

tests/TDBMDaoGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ public function testFloydHasNoParent(): void
19971997
{
19981998
$artists = new ArtistDao($this->tdbmService);
19991999
$pinkFloyd = $artists->getById(1);
2000-
$parents = $pinkFloyd->getArtistsByArtistsRelationsViaChildId();
2000+
$parents = $pinkFloyd->getParents();
20012001

20022002
$this->assertEquals([], $parents);
20032003
}
@@ -2006,7 +2006,7 @@ public function testFloydHasOneChild(): void
20062006
{
20072007
$artists = new ArtistDao($this->tdbmService);
20082008
$pinkFloyd = $artists->getById(1);
2009-
$children = $pinkFloyd->getArtistsByArtistsRelationsViaParentId();
2009+
$children = $pinkFloyd->getChildren();
20102010

20112011
$this->assertEquals(1, count($children));
20122012
$this->assertEquals(2, $children[0]->getId());

0 commit comments

Comments
 (0)