Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Propel Generator: for getManyToManyTables use real ManyToMany, fixes #88

  • Loading branch information...
commit fc97215a4e9ca8579123b3405b7bf43628fbe589 1 parent 091d954
@jaugustin authored
View
57 lib/generator/sfPropelFormGenerator.class.php
@@ -124,35 +124,48 @@ public function generate($params = array())
public function getManyToManyTables()
{
$tables = array();
+ $foreignTables = array();
+ $relations = array();
- // go through all tables to find m2m relationships
- foreach ($this->dbMap->getTables() as $tableName => $table)
+ // go through all relations
+ foreach ($this->table->getRelations() as $relation)
+ {
+ //we have a many to many Relation
+ if (RelationMap::MANY_TO_MANY === $relation->getType())
+ {
+ $foreignTables[$relation->getLocalTable()->getClassname()] = $relation->getLocalTable();
+ }
+ else if (RelationMap::ONE_TO_MANY === $relation->getType())
+ {
+ $relations[$relation->getLocalTable()->getClassname()] = $relation;
+ }
+ }
+
+ // find middleTable for Many to Many relation
+ foreach ($foreignTables as $tableName => $foreignTable)
{
- foreach ($table->getColumns() as $column)
+ foreach ($foreignTable->getRelations() as $foreignRelation)
{
- if ($column->isForeignKey() && $column->isPrimaryKey() && $this->table->getClassname() == $this->getForeignTable($column)->getClassname())
+ $foreignTableName = $foreignRelation->getLocalTable()->getClassname();
+
+ // Test if the foreign table has a common relation with our table
+ // TODO: test if is CrossRef
+ if (RelationMap::ONE_TO_MANY === $foreignRelation->getType()
+ && array_key_exists($foreignTableName, $relations))
{
- // we have a m2m relationship
- // find the other primary key
- foreach ($table->getColumns() as $relatedColumn)
- {
- if ($relatedColumn->isForeignKey() && $relatedColumn->isPrimaryKey() && $this->table->getClassname() != $this->getForeignTable($relatedColumn)->getClassname())
- {
- // we have the related table
- $tables[] = array(
- 'middleTable' => $table,
- 'relatedTable' => $this->getForeignTable($relatedColumn),
- 'column' => $column,
- 'relatedColumn' => $relatedColumn,
- );
-
- break 2;
- }
- }
+ $columns = $relations[$foreignTableName]->getLocalColumns();
+ $relatedColumns = $foreignRelation->getLocalColumns();
+
+ $tables[] = array(
+ 'middleTable' => $foreignRelation->getLocalTable(),
+ 'relatedTable' => $foreignTable,
+ 'column' => reset($columns),
+ 'relatedColumn' => reset($relatedColumns),
+ );
+ continue 2;
}
}
}
-
return $tables;
}
View
58 lib/generator/sfPropelGenerator.class.php
@@ -66,38 +66,48 @@ public function getTableMap()
public function getManyToManyTables()
{
$tables = array();
+ $foreignTables = array();
+ $relations = array();
- // go through all tables to find m2m relationships
- foreach ($this->dbMap->getTables() as $tableName => $table)
+ // go through all relations
+ foreach ($this->getTableMap()->getRelations() as $relation)
{
- // load this table's relations and related tables
- $table->getRelations();
+ //we have a many to many Relation
+ if (RelationMap::MANY_TO_MANY === $relation->getType())
+ {
+ $foreignTables[$relation->getLocalTable()->getClassname()] = $relation->getLocalTable();
+ }
+ else if (RelationMap::ONE_TO_MANY === $relation->getType())
+ {
+ $relations[$relation->getLocalTable()->getClassname()] = $relation;
+ }
+ }
- foreach ($table->getColumns() as $column)
+ // find middleTable for Many to Many relation
+ foreach ($foreignTables as $tableName => $foreignTable)
+ {
+ foreach ($foreignTable->getRelations() as $foreignRelation)
{
- if ($column->isForeignKey() && $column->isPrimaryKey() && $this->getTableMap()->getClassname() == $this->dbMap->getTable($column->getRelatedTableName())->getClassname())
+ $foreignTableName = $foreignRelation->getLocalTable()->getClassname();
+
+ // Test if the foreign table has a common relation with our table
+ // TODO: test if is CrossRef
+ if (RelationMap::ONE_TO_MANY === $foreignRelation->getType()
+ && array_key_exists($foreignTableName, $relations))
{
- // we have a m2m relationship
- // find the other primary key
- foreach ($table->getColumns() as $relatedColumn)
- {
- if ($relatedColumn->isForeignKey() && $relatedColumn->isPrimaryKey() && $this->getTableMap()->getClassname() != $this->dbMap->getTable($relatedColumn->getRelatedTableName())->getClassname())
- {
- // we have the related table
- $tables[] = array(
- 'middleTable' => $table,
- 'relatedTable' => $this->dbMap->getTable($relatedColumn->getRelatedTableName()),
- 'column' => $column,
- 'relatedColumn' => $relatedColumn,
- );
-
- break 2;
- }
- }
+ $columns = $relations[$foreignTableName]->getLocalColumns();
+ $relatedColumns = $foreignRelation->getLocalColumns();
+
+ $tables[] = array(
+ 'middleTable' => $foreignRelation->getLocalTable(),
+ 'relatedTable' => $foreignTable,
+ 'column' => reset($columns),
+ 'relatedColumn' => reset($relatedColumns),
+ );
+ continue 2;
}
}
}
-
return $tables;
}
Please sign in to comment.
Something went wrong with that request. Please try again.