1
0
mirror of synced 2024-12-14 07:06:04 +03:00

[DDC-1617] Implement support for Generating Unique Constraints/Indexes in @Table annotation of EntityGenerator.

This commit is contained in:
Benjamin Eberlei 2012-01-28 11:16:36 +01:00
parent 35fc3c0671
commit 551df4af52
2 changed files with 25 additions and 0 deletions

View File

@ -583,9 +583,32 @@ public function <methodName>()
$table[] = 'name="' . $metadata->table['name'] . '"'; $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) . ')'; 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) private function _generateInheritanceAnnotation($metadata)
{ {
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) { if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {

View File

@ -47,6 +47,8 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository'; $metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';
$metadata->table['name'] = 'book'; $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' => 'name', 'type' => 'string'));
$metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published')); $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));