8000 bug #9222 [Bridge] [Propel1] Fixed guessed relations (ClementGautier) · symfony/symfony@6659d7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 6659d7d

Browse files
committed
bug #9222 [Bridge] [Propel1] Fixed guessed relations (ClementGautier)
This PR was merged into the 2.2 branch. Discussion ---------- [Bridge] [Propel1] Fixed guessed relations <table> <tr><th>Q</th><th>A</th></tr> <tr><td>Bug fix</td><td>yes</td></tr> <tr><td>Feature addition</td><td>no</td></tr> <tr><td>Backwards compatibility break</td><td>no</td></tr> <tr><td>Symfony2 tests pass</td><td>yes</td></tr> <tr><td>Fixes the following tickets</td><td>N/A</td></tr> <tr><td>Todo</td><td>N/A</td></tr> <tr><td>License of the code</td><td>MIT</td></tr> <tr><td>Documentation PR</td><td>N/A</td></tr> </table> The `PropelTypeGuesser` did not match OneToMany relations properly. For example if you have a `Author` class with `Comments` attributes the guesser would guess "Comment" instead of "Comments". I added some tests to ensure the non regression. Commits ------- 0b1e95f Fixed propel guessed relations
2 parents e410b1f + 0b1e95f commit 6659d7d

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php

Lines changed: 10 additions & 3 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ public function guessType($class, $property)
3535
}
3636

3737
foreach ($table->getRelations() as $relation) {
38-
if (in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE, \RelationMap::ONE_TO_MANY))) {
39-
if ($property == $relation->getForeignTable()->getName()) {
38+
if ($relation->getType() === \RelationMap::MANY_TO_ONE) {
39+
if (strtolower($property) === strtolower($relation->getName())) {
4040
return new TypeGuess('model', array(
4141
'class' => $relation->getForeignTable()->getClassName(),
42-
'multiple' => \RelationMap::MANY_TO_ONE === $relation->getType() ? false : true,
42+
'multiple' => false,
43+
), Guess::HIGH_CONFIDENCE);
44+
}
45+
} elseif ($relation->getType() === \RelationMap::ONE_TO_MANY) {
46+
if (strtolower($property) === strtolower($relation->getPluralName())) {
47+
return new TypeGuess('model', array(
48+
'class' => $relation->getForeignTable()->getClassName(),
49+
'multiple' => true,
4350
), Guess::HIGH_CONFIDENCE);
4451
}
4552
} elseif ($relation->getType() === \RelationMap::MANY_TO_MANY) {

src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class ItemQuery
2020
'is_active' => \PropelColumnTypes::BOOLEAN,
2121
'enabled' => \PropelColumnTypes::BOOLEAN_EMU,
2222
'updated_at' => \PropelColumnTypes::TIMESTAMP,
23+
24+
'updated_at' => \PropelColumnTypes::TIMESTAMP,
25+
'updated_at' => \PropelColumnTypes::TIMESTAMP,
26+
'updated_at' => \PropelColumnTypes::TIMESTAMP,
2327
);
2428

2529
public function getTableMap()
@@ -62,6 +66,30 @@ public function getColumn($column)
6266
*/
6367
public function getRelations()
6468
{
65-
return array();
69+
// table maps
70+
$authorTable = new \TableMap();
71+
$authorTable->setClassName('\Foo\Author');
72+
73+
$resellerTable = new \TableMap();
74+
$resellerTable->setClassName('\Foo\Reseller');
75+
76+
// relations
77+
$mainAuthorRelation = new \RelationMap('MainAuthor');
78+
$mainAuthorRelation->setType(\RelationMap::MANY_TO_ONE);
79+
$mainAuthorRelation->setForeignTable($authorTable);
80+
81+
$authorRelation = new \RelationMap('Author');
82+
$authorRelation->setType(\RelationMap::ONE_TO_MANY);
83+
$authorRelation->setForeignTable($authorTable);
84+
85+
$resellerRelation = new \RelationMap('Reseller');
86+
$resellerRelation->setType(\RelationMap::MANY_TO_MANY);
87+
$resellerRelation->setLocalTable($resellerTable);
88+
89+
return array(
90+
$mainAuthorRelation,
91+
$authorRelation,
92+
$resellerRelation
93+
);
6694
}
6795
}

src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ public function testGuessTypeWithoutColumn()
9696
/**
9797
* @dataProvider dataProviderForGuessType
9898
*/
99-
public function testGuessType($property, $type, $confidence)
99+
public function testGuessType($property, $type, $confidence, $multiple = null)
100100
{
101101
$value = $this->guesser->guessType(self::CLASS_NAME, $property);
102102

103103
$this->assertNotNull($value);
104104
$this->assertEquals($type, $value->getType());
105105
$this->assertEquals($confidence, $value->getConfidence());
106+
107+
if ($type === 'model') {
108+
$options = $value->getOptions();
109+
110+
$this->assertSame($multiple, $options['multiple']);
111+
}
106112
}
107113

108114
public static function dataProviderForGuessType()
@@ -114,6 +120,10 @@ public static function dataProviderForGuessType()
114120
array('value', 'text', Guess::MEDIUM_CONFIDENCE),
115121
array('price', 'number', Guess::MEDIUM_CONFIDENCE),
116122
array('updated_at', 'datetime', Guess::HIGH_CONFIDENCE),
123+
124+
array('Authors', 'model', Guess::HIGH_CONFIDENCE, true),
125+
array('Resellers', 'model', Guess::HIGH_CONFIDENCE, true),
126+
array('MainAuthor', 'model', Guess::HIGH_CONFIDENCE, false),
117127
);
118128
}
119129
}

0 commit comments

Comments
 (0)
0