Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix many-to-many form generation when middle table join one table twice,

(only without isCrossRef=true) issues #130 #129
  • Loading branch information...
commit 15e733a2f8bd86c5d7b6d74583b317790bd64cd3 1 parent e58544b
@jaugustin jaugustin authored
View
2  data/generator/sfPropelForm/default/template/sfPropelFormGeneratedTemplate.php
@@ -78,7 +78,7 @@ public function updateDefaultsFromObject()
if (isset($this->widgetSchema['<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list']))
{
$values = array();
- foreach ($this->object->get<?php echo $tables['middleTable']->getPhpName() ?>s() as $obj)
+ foreach ($this->object-><?php echo $tables['relatedGetter'] ?>() as $obj) //Old: $this->object->get<?php echo $tables['middleTable']->getPhpName() ?>s()
{
$values[] = $obj->get<?php echo $tables['relatedColumn']->getPhpName() ?>();
}
View
14 lib/generator/sfPropelFormGenerator.class.php
@@ -134,7 +134,7 @@ public function getManyToManyTables()
//we have a many to many Relation
if (RelationMap::MANY_TO_MANY === $relation->getType())
{
- $foreignTables[$relation->getLocalTable()->getClassname()] = $relation->getLocalTable();
+ $foreignTables[$relation->getLocalTable()->getClassname()] = $relation;
}
else if (RelationMap::ONE_TO_MANY === $relation->getType())
{
@@ -143,8 +143,9 @@ public function getManyToManyTables()
}
// find middleTable for Many to Many relation
- foreach ($foreignTables as $tableName => $foreignTable)
+ foreach ($foreignTables as $tableName => $relation)
{
+ $foreignTable = $relation->getLocalTable();
foreach ($foreignTable->getRelations() as $foreignRelation)
{
$foreignTableClassname = $foreignRelation->getLocalTable()->getClassname();
@@ -157,11 +158,13 @@ public function getManyToManyTables()
$relatedColumns = $foreignRelation->getLocalColumns();
$middleTable = $foreignRelation->getLocalTable();
- $middleTables[$middleTable->getClassname()] = $middleTable;
- if ($middleTable->isCrossRef())
+ if ($middleTable->isCrossRef() && !isset($middleTables[$middleTable->getClassname()]))
{
+ // Add this middleTable to table list to prevent using it twice
+ $middleTables[$middleTable->getClassname()] = $middleTable;
$tables[] = array(
'middleTable' => $middleTable,
+ 'relatedGetter' => $foreignTable->getPhpname() == $relation->getName() ? 'get' . $middleTable->getPhpname() . 's' : 'get' . $relation->getPluralName(),
'relatedTable' => $foreignTable,
'column' => reset($columns),
'relatedColumn' => reset($relatedColumns),
@@ -191,9 +194,12 @@ public function getManyToManyTables()
$foreignTable = $pks[0]->getRelatedTableName() != $this->table->getName() ? $pks[0]->getRelatedTable() : $pks[1]->getRelatedTable();
$relatedColumn = $pks[0]->getRelatedTableName() != $this->table->getName() ? $pks[0] : $pks[1];
$columns = $relation->getLocalColumns();
+ // Add this middleTable to table list to prevent using it twice
+ $middleTables[$middleTable->getClassname()] = $middleTable;
$tables[] = array(
'middleTable' => $middleTable,
+ 'relatedGetter' => $middleTable->getPhpname() == $relation->getName() ? 'get' . $middleTable->getPhpname() . 's' : 'get' . $relation->getPluralName(),
'relatedTable' => $foreignTable,
'column' => reset($columns),
'relatedColumn' => $relatedColumn,
View
12 lib/generator/sfPropelGenerator.class.php
@@ -77,7 +77,7 @@ public function getManyToManyTables()
//we have a many to many Relation
if (RelationMap::MANY_TO_MANY === $relation->getType())
{
- $foreignTables[$relation->getLocalTable()->getClassname()] = $relation->getLocalTable();
+ $foreignTables[$relation->getLocalTable()->getClassname()] = $relation;
}
else if (RelationMap::ONE_TO_MANY === $relation->getType())
{
@@ -86,8 +86,9 @@ public function getManyToManyTables()
}
// find middleTable for Many to Many relation
- foreach ($foreignTables as $tableName => $foreignTable)
+ foreach ($foreignTables as $tableName => $relation)
{
+ $foreignTable = $relation->getLocalTable();
foreach ($foreignTable->getRelations() as $foreignRelation)
{
$foreignTableClassname = $foreignRelation->getLocalTable()->getClassname();
@@ -100,9 +101,10 @@ public function getManyToManyTables()
$relatedColumns = $foreignRelation->getLocalColumns();
$middleTable = $foreignRelation->getLocalTable();
- $middleTables[$middleTable->getClassname()] = $middleTable;
- if ($middleTable->isCrossRef())
+ if ($middleTable->isCrossRef() && !isset($middleTables[$middleTable->getClassname()]))
{
+ // Add this middleTable to table list to prevent using it twice
+ $middleTables[$middleTable->getClassname()] = $middleTable;
$tables[] = array(
'middleTable' => $middleTable,
'relatedTable' => $foreignTable,
@@ -134,6 +136,8 @@ public function getManyToManyTables()
$foreignTable = $pks[0]->getRelatedTableName() != $table->getName() ? $pks[0]->getRelatedTable() : $pks[1]->getRelatedTable();
$relatedColumn = $pks[0]->getRelatedTableName() != $table->getName() ? $pks[0] : $pks[1];
$columns = $relation->getLocalColumns();
+ // Add this middleTable to table list to prevent using it twice
+ $middleTables[$middleTable->getClassname()] = $middleTable;
$tables[] = array(
'middleTable' => $middleTable,
View
55 test/functional/fixtures/config/schema.xml
@@ -181,6 +181,61 @@
</foreign-key>
</table>
+ <table name="extra_seller_without_crossref">
+ <column name="seller_id" type="integer" primaryKey="true" />
+ <column name="extra_id" type="integer" primaryKey="true" />
+ <foreign-key foreignTable="seller">
+ <reference local="seller_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="extra">
+ <reference local="extra_id" foreign="id" />
+ </foreign-key>
+ </table>
+
+ <table name="extra_seller_without_crossref2">
+ <column name="seller_id" type="integer" primaryKey="true" />
+ <column name="extra_id" type="integer" primaryKey="true" />
+ <foreign-key foreignTable="seller">
+ <reference local="seller_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="extra">
+ <reference local="extra_id" foreign="id" />
+ </foreign-key>
+ </table>
+
+ <table name="book_seller_without_crossref">
+ <column name="seller_id" type="integer" primaryKey="true" />
+ <column name="book_id" type="integer" primaryKey="true" />
+ <foreign-key foreignTable="seller">
+ <reference local="seller_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="book">
+ <reference local="book_id" foreign="id" />
+ </foreign-key>
+ </table>
+
+ <table name="seller_seller" isCrossRef="true">
+ <column name="seller_id" type="integer" primaryKey="true" />
+ <column name="seller_cross_id" type="integer" primaryKey="true" />
+ <foreign-key foreignTable="seller">
+ <reference local="seller_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="seller">
+ <reference local="seller_cross_id" foreign="id" />
+ </foreign-key>
+ </table>
+
+ <table name="seller_seller2">
+ <column name="seller_id" type="integer" primaryKey="true" />
+ <column name="seller_cross_id" type="integer" primaryKey="true" />
+ <foreign-key foreignTable="seller">
+ <reference local="seller_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="seller">
+ <reference local="seller_cross_id" foreign="id" />
+ </foreign-key>
+ </table>
+
<table name="extra">
<column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" />
<column name="name" type="varchar" size="255" />
Please sign in to comment.
Something went wrong with that request. Please try again.