Skip to content

Commit

Permalink
[DDC-1617] Implement support for Generating Unique Constraints/Indexe…
Browse files Browse the repository at this point in the history
…s in @table annotation of EntityGenerator.
  • Loading branch information
beberlei committed Jan 28, 2012
1 parent 35fc3c0 commit 551df4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Doctrine/ORM/Tools/EntityGenerator.php
Expand Up @@ -583,9 +583,32 @@ private function _generateTableAnnotation($metadata)
$table[] = 'name="' . $metadata->table['name'] . '"';
}

if (isset($metadata->table['uniqueConstraints']) && $metadata->table['uniqueConstraints']) {
$constraints = $this->_generateTableConstraints('UniqueConstraint', $metadata->table['uniqueConstraints']);
$table[] = 'uniqueConstraints={' . $constraints . '}';
}

if (isset($metadata->table['indexes']) && $metadata->table['indexes']) {
$constraints = $this->_generateTableConstraints('Index', $metadata->table['indexes']);
$table[] = 'indexes={' . $constraints . '}';
}

return '@' . $this->_annotationsPrefix . 'Table(' . implode(', ', $table) . ')';
}

private function _generateTableConstraints($constraintName, $constraints)
{
$annotations = array();
foreach ($constraints as $name => $constraint) {
$columns = array();
foreach ($constraint['columns'] as $column) {
$columns[] = '"' . $column . '"';
}
$annotations[] = '@' . $this->_annotationsPrefix . $constraintName . '(name="' . $name . '", columns={' . implode(', ', $columns) . '})';
}
return implode(', ', $annotations);
}

private function _generateInheritanceAnnotation($metadata)
{
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php
Expand Up @@ -47,6 +47,8 @@ public function generateBookEntityFixture()
$metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';

$metadata->table['name'] = 'book';
$metadata->table['uniqueConstraints']['name_uniq'] = array('columns' => array('name'));
$metadata->table['indexes']['status_idx'] = array('columns' => array('status'));
$metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
$metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
Expand Down

0 comments on commit 551df4a

Please sign in to comment.