Skip to content

Commit 213bb52

Browse files
committed
Merge pull request #44 from keyermann/master
Custom @Orm\Column annotation
2 parents 50abcc0 + ed9df4b commit 213bb52

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,44 @@ public function generateFieldAnnotations($className, $fieldName)
106106
$annotation = '@ORM\Column';
107107
$isColumnHasProperties = false;
108108

109-
if ($type !== 'string' || $field['isNullable'] || $field['isUnique']) {
110-
$isColumnHasProperties = true;
111-
}
109+
if ($field['ormColumn']) {
110+
$annotation .= '('.$field['ormColumn'].')';
111+
} else {
112+
if ($type !== 'string' || $field['isNullable'] || $field['isUnique']) {
113+
$isColumnHasProperties = true;
114+
}
112115

113-
if ($field['isArray']) {
114-
$type = 'simple_array';
115-
}
116+
if ($field['isArray']) {
117+
$type = 'simple_array';
118+
}
116119

117-
if ($isColumnHasProperties) {
118-
$annotation .= '(';
119-
}
120+
if ($isColumnHasProperties) {
121+
$annotation .= '(';
122+
}
120123

121-
if ($type !== 'string') {
122-
$annotation .= sprintf('type="%s"', $type);
123-
}
124+
if ($type !== 'string') {
125+
$annotation .= sprintf('type="%s"', $type);
126+
}
124127

125-
if ($type !== 'string' && $field['isNullable']) {
126-
$annotation .= ', ';
127-
}
128+
if ($type !== 'string' && $field['isNullable']) {
129+
$annotation .= ', ';
130+
}
128131

129-
if ($field['isNullable']) {
130-
$annotation .= 'nullable=true';
131-
}
132+
if ($field['isNullable']) {
133+
$annotation .= 'nullable=true';
134+
}
132135

133-
if ($field['isUnique'] && $field['isNullable']) {
134-
$annotation .= ', ';
135-
}
136+
if ($field['isUnique'] && $field['isNullable']) {
137+
$annotation .= ', ';
138+
}
136139

137-
if ($field['isUnique']) {
138-
$annotation .= 'unique=true';
139-
}
140+
if ($field['isUnique']) {
141+
$annotation .= 'unique=true';
142+
}
140143

141-
if ($isColumnHasProperties) {
142-
$annotation .= ')';
144+
if ($isColumnHasProperties) {
145+
$annotation .= ')';
146+
}
143147
}
144148

145149
$annotations[] = $annotation;

src/TypesGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ public function generate($config)
303303
'resource' => null,
304304
'range' => 'Integer',
305305
'cardinality' => CardinalitiesExtractor::CARDINALITY_1_1,
306+
'ormColumn' => null,
306307
'isArray' => false,
307308
'isNullable' => false,
308309
'isUnique' => false,
@@ -587,6 +588,8 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
587588
$cardinality = $property ? $this->cardinalities[$propertyName] : CardinalitiesExtractor::CARDINALITY_1_1;
588589
}
589590

591+
$ormColumn = isset($propertyConfig['ormColumn']) ? $propertyConfig['ormColumn'] : null;
592+
590593
$isArray = in_array($cardinality, [
591594
CardinalitiesExtractor::CARDINALITY_1_N,
592595
CardinalitiesExtractor::CARDINALITY_N_N,
@@ -613,6 +616,7 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
613616
'resource' => $property,
614617
'range' => $ranges[0],
615618
'cardinality' => $cardinality,
619+
'ormColumn' => $ormColumn,
616620
'isArray' => $isArray,
617621
'isNullable' => $isNullable,
618622
'isUnique' => isset($propertyConfig['unique']) && $propertyConfig['unique'],
@@ -796,8 +800,7 @@ private function generateClassUses($annotationGenerators, $classes, $className)
796800
{
797801
$uses = $classes[$className]['uses'];
798802

799-
if (
800-
isset($classes[$className]['interfaceNamespace'])
803+
if (isset($classes[$className]['interfaceNamespace'])
801804
&& $classes[$className]['interfaceNamespace'] !== $classes[$className]['namespace']
802805
) {
803806
$uses[] = sprintf(

src/TypesGeneratorConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function ($rdfa) {
129129
CardinalitiesExtractor::CARDINALITY_N_N,
130130
CardinalitiesExtractor::CARDINALITY_UNKNOWN,
131131
])->end()
132+
->scalarNode('ormColumn')->defaultNull()->info('The doctrine column annotation content')->example('type="decimal", precision=5, scale=1, options={"comment" = "my comment"}')->end()
132133
->arrayNode('groups')
133134
->info('Symfony Serialization Groups')
134135
->prototype('scalar')->end()

tests/config/address-book.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ types:
1818
additionalName: ~
1919
gender: ~
2020
address: { range: PostalAddress }
21-
birthDate: ~
21+
# Custom range and custom ORM\Column content
22+
birthDate: { range: DateTime, ormColumn: 'type="datetimetz", nullable=true, options={"comment" = "Birthdate with timezone."}' }
2223
telephone: ~
2324
email: ~
2425
jobTitle: ~
@@ -45,3 +46,5 @@ types:
4546
parent: false
4647
properties:
4748
name: ~
49+
# Custom property with custom ORM\Column content
50+
adminCode: {range: Text, ormColumn: 'type="string", length=3, unique=true, nullable=false, options={"comment" = "A code for central administration."}' }

0 commit comments

Comments
 (0)