8000 [Bridge] [Propel1] Fixed guessed relations by ClementGautier · Pull Request #9222 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Bridge] [Propel1] Fixed guessed relations #9222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ public function guessType($class, $property)
}

foreach ($table->getRelations() as $relation) {
if (in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE, \RelationMap::ONE_TO_MANY))) {
if ($property == $relation->getForeignTable()->getName()) {
if ($relation->getType() === \RelationMap::MANY_TO_ONE) {
if (strtolower($property) === strtolower($relation->getName())) {
return new TypeGuess('model', array(
'class' => $relation->getForeignTable()->getClassName(),
'multiple' => \RelationMap::MANY_TO_ONE === $relation->getType() ? false : true,
'multiple' => false,
), Guess::HIGH_CONFIDENCE);
}
} elseif ($relation->getType() === \RelationMap::ONE_TO_MANY) {
if (strtolower($property) === strtolower($relation->getPluralName())) {
return new TypeGuess('model', array(
'class' => $relation->getForeignTable()->getClassName(),
'multiple' => true,
), Guess::HIGH_CONFIDENCE);
}
} elseif ($relation->getType() === \RelationMap::MANY_TO_MANY) {
Expand Down
30 changes: 29 additions & 1 deletion src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ItemQuery
'is_active' => \PropelColumnTypes::BOOLEAN,
'enabled' => \PropelColumnTypes::BOOLEAN_EMU,
'updated_at' => \PropelColumnTypes::TIMESTAMP,

'updated_at' => \PropelColumnTypes::TIMESTAMP,
'updated_at' => \PropelColumnTypes::TIMESTAMP,
'updated_at' => \PropelColumnTypes::TIMESTAMP,
);

public function getTableMap()
Expand Down Expand Up @@ -62,6 +66,30 @@ public function getColumn($column)
*/
public function getRelations()
{
return array();
// table maps
$authorTable = new \TableMap();
$authorTable->setClassName('\Foo\Author');

$resellerTable = new \TableMap();
$resellerTable->setClassName('\Foo\Reseller');

// relations
$mainAuthorRelation = new \RelationMap('MainAuthor');
$mainAuthorRelation->setType(\RelationMap::MANY_TO_ONE);
$mainAuthorRelation->setForeignTable($authorTable);

$authorRelation = new \RelationMap('Author');
$authorRelation->setType(\RelationMap::ONE_TO_MANY);
$authorRelation->setForeignTable($authorTable);

$resellerRelation = new \RelationMap('Reseller');
$resellerRelation->setType(\RelationMap::MANY_TO_MANY);
$resellerRelation->setLocalTable($resellerTable);

return array(
$mainAuthorRelation,
$authorRelation,
$resellerRelation
);
}
}
12 changes: 11 additions & 1 deletion src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ public function testGuessTypeWithoutColumn()
/**
* @dataProvider dataProviderForGuessType
*/
public function testGuessType($property, $type, $confidence)
public function testGuessType($property, $type, $confidence, $multiple = null)
{
$value = $this->guesser->guessType(self::CLASS_NAME, $property);

$this->assertNotNull($value);
$this->assertEquals($type, $value->getType());
$this->assertEquals($confidence, $value->getConfidence());

if ($type === 'model') {
$options = $value->getOptions();

$this->assertSame($multiple, $options['multiple']);
}
}

public static function dataProviderForGuessType()
Expand All @@ -114,6 +120,10 @@ public static function dataProviderForGuessType()
array('value', 'text', Guess::MEDIUM_CONFIDENCE),
array('price', 'number', Guess::MEDIUM_CONFIDENCE),
array('updated_at', 'datetime', Guess::HIGH_CONFIDENCE),

array('Authors', 'model', Guess::HIGH_CONFIDENCE, true),
array('Resellers', 'model', Guess::HIGH_CONFIDENCE, true),
array('MainAuthor', 'model', Guess::HIGH_CONFIDENCE, false),
);
}
}
0