Skip to content

Commit b578cc0

Browse files
authored
Add support for default value (#167)
* Add support for default value
1 parent 4a20959 commit b578cc0

16 files changed

+219
-33
lines changed

DBAL/Types/AbstractEnumType.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ static function (string $value) {
112112
$sqlDeclaration = \sprintf('ENUM(%s)', $values);
113113
}
114114

115+
$defaultValue = static::getDefaultValue();
116+
if (null !== $defaultValue) {
117+
$sqlDeclaration .= \sprintf(' DEFAULT %s', $platform->quoteStringLiteral($defaultValue));
118+
}
119+
115120
return $sqlDeclaration;
116121
}
117122

@@ -136,7 +141,7 @@ public function getName(): string
136141
*
137142
* @static
138143
*
139-
* @return string[] Values for the ENUM field
144+
* @return string[]
140145
*/
141146
public static function getChoices(): array
142147
{
@@ -175,7 +180,7 @@ public static function getRandomValue()
175180
*
176181
* @static
177182
*
178-
* @return string[] Array of values with readable format
183+
* @return string[] Array of values in readable format
179184
*/
180185
public static function getReadableValues(): array
181186
{
@@ -203,7 +208,7 @@ public static function assertValidChoice(string $value): void
203208
*
204209
* @static
205210
*
206-
* @return string $value Value in readable format
211+
* @return string Value in readable format
207212
*/
208213
public static function getReadableValue(string $value): string
209214
{
@@ -226,6 +231,18 @@ public static function isValueExist(string $value): bool
226231
return isset(static::$choices[$value]);
227232
}
228233

234+
/**
235+
* Get default value for DDL statement.
236+
*
237+
* @static
238+
*
239+
* @return string|null Default value for DDL statement
240+
*/
241+
public static function getDefaultValue(): ?string
242+
{
243+
return null;
244+
}
245+
229246
/**
230247
* Gets an array of database types that map to this Doctrine type.
231248
*

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
| Bundle Version (X.Y.Z) | PHP | Symfony | Doctrine Bundle | Comment |
2626
|:----------------------:|:----------------:|:----------------:|:------------------:|:--------------------------|
27-
| `7.0.*` | `>= 7.2.5` | `>= 5.0` | `>= 2.0` | **Current version** |
28-
| `6.6.*` | `>= 7.1.3` | `4.3, 4.4` | `> 1.11`, `>= 2.0` | Bugfix support |
27+
| `7.1.*` | `>= 7.2.5` | `>= 5.0` | `>= 2.0` | **Current version** |
28+
| `6.6.*` | `>= 7.1.3` | `4.3, 4.4` | `> 1.11`, `>= 2.0` | Previous version |
2929

3030
#### Check the `config/bundles.php` file
3131

@@ -49,6 +49,7 @@ return [
4949
## Features 🎁
5050

5151
* [NULL values](./Resources/docs/null_values.md "NULL values")
52+
* [Default value](./Resources/docs/default_value.md "Default value")
5253
* [Building the form](./Resources/docs/building_the_form.md "Building the form")
5354
* [Additional methods](./Resources/docs/additional_methods.md "Additional methods")
5455
* [Common types](./Resources/docs/common_types.md "Common types")

Resources/docs/additional_methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ BasketballPositionType::getValues();
2323
// Will return: ['PG', 'SG', 'SF', 'PF', 'C']
2424
```
2525

26-
---
27-
2826
##### If you need to return a random value:
2927

3028
```php
@@ -37,7 +35,9 @@ BasketballPositionType::getRandomValue();
3735
### More features
3836

3937
* [NULL values](./null_values.md "NULL values")
38+
* [Default value](./default_value.md "Default value")
4039
* [Building the form](./building_the_form.md "Building the form")
40+
* [Common types](./common_types.md "Common types")
4141
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
4242
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
4343
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")

Resources/docs/building_the_form.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ If you need to add some extra parameters, just skip the second *field type* para
1212
$builder->add('position', null, [
1313
'required' => true,
1414
'attr' => [
15-
'class' => 'some-class'
16-
]
15+
'class' => 'some-class',
16+
],
1717
]);
1818
```
1919

@@ -23,7 +23,7 @@ If for some reason you need to specify full config, it can look like this:
2323
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
2424

2525
$builder->add('position', ChoiceType::class, [
26-
'choices' => BasketballPositionType::getChoices()
26+
'choices' => BasketballPositionType::getChoices(),
2727
]);
2828
```
2929

@@ -35,7 +35,9 @@ All other custom DBAL types, which are defined, will be skipped from guessing.
3535
### More features
3636

3737
* [NULL values](./null_values.md "NULL values")
38+
* [Default value](./default_value.md "Default value")
3839
* [Additional methods](./additional_methods.md "Additional methods")
40+
* [Common types](./common_types.md "Common types")
3941
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
4042
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
4143
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")

Resources/docs/common_types.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@
55
[MonthFullNameType](./../../DBAL/Types/MonthFullNameType.php "MonthFullNameType"): january, february, march, etc.
66
[MonthShortNameType](./../../DBAL/Types/MonthShortNameType.php "MonthShortNameType"): jan, feb, mar, etc.
77

8+
---
9+
10+
### More features
11+
12+
* [NULL values](./null_values.md "NULL values")
13+
* [Default value](./default_value.md "Default value")
14+
* [Additional methods](./additional_methods.md "Additional methods")
15+
* [Building the form](./building_the_form.md "Building the form")
16+
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
17+
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
18+
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")
19+
* [Hook for Doctrine migrations](./hook_for_doctrine_migrations.md "Hook for Doctrine migrations")

Resources/docs/default_value.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Default value
2+
3+
Override method `getDefaultValue` in your ENUM class:
4+
5+
```php
6+
<?php
7+
namespace App\DBAL\Types;
8+
9+
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
10+
11+
final class MapLocationType extends AbstractEnumType
12+
{
13+
public const NORTH = 'N';
14+
public const EAST = 'E';
15+
public const SOUTH = 'S';
16+
public const WEST = 'W';
17+
public const CENTER = 'C';
18+
19+
protected static $choices = [
20+
self::NORTH => 'North',
21+
self::EAST => 'East',
22+
self::SOUTH => 'South',
23+
self::WEST => 'West',
24+
self::CENTER => 'Center',
25+
];
26+
27+
public static function getDefaultValue(): ?string
28+
{
29+
return self::CENTER; // This value will be used as default in DDL statement
30+
}
31+
}
32+
```
33+
34+
---
35+
36+
### More features
37+
38+
* [NULL values](./null_values.md "NULL values")
39+
* [Building the form](./building_the_form.md "Building the form")
40+
* [Additional methods](./additional_methods.md "Additional methods")
41+
* [Common types](./common_types.md "Common types")
42+
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
43+
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
44+
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")
45+
* [Hook for Doctrine migrations](./hook_for_doctrine_migrations.md "Hook for Doctrine migrations")

Resources/docs/enum_constants_in_templates.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
There is also another custom TWIG filter `|enum_constant`. It allows to use constants from ENUM classes in templates to print their values or to compare with other values.
44

5-
```jinja
5+
```twig
66
{{ 'SHOOTING_GUARD'|enum_constant }}
77
{{ 'NORTH_WEST'|enum_constant }}
88
@@ -14,7 +14,7 @@ There is also another custom TWIG filter `|enum_constant`. It allows to use cons
1414
Same problem as for `|readable_enum` filter is present here too. If some constant is defined in few ENUM classes then an exception will be thrown.
1515
You can specify the correct class for this constant and it solves the problem.
1616

17-
```jinja
17+
```twig
1818
{{ 'CENTER'|enum_constant('BasketballPositionType') }}
1919
{{ 'CENTER'|enum_constant('MapLocationType') }}
2020
```
@@ -24,8 +24,10 @@ You can specify the correct class for this constant and it solves the problem.
2424
### More features
2525

2626
* [NULL values](./null_values.md "NULL values")
27+
* [Default value](./default_value.md "Default value")
2728
* [Building the form](./building_the_form.md "Building the form")
2829
* [Additional methods](./additional_methods.md "Additional methods")
30+
* [Common types](./common_types.md "Common types")
2931
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
3032
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")
3133
* [Hook for Doctrine migrations](./hook_for_doctrine_migrations.md "Hook for Doctrine migrations")

Resources/docs/enum_values_in_templates.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ They are `enum_values()` and `enum_readable_values()`.
55

66
#### Example of using `enum_values()`
77

8-
```jinja
8+
```twig
99
{% for value in enum_values('BasketballPositionType') %}
1010
{{ value }}<br />
1111
{% endfor %}
@@ -22,7 +22,7 @@ C
2222

2323
#### Example of using `enum_readable_values()`
2424

25-
```jinja
25+
```twig
2626
{% for key, value in enum_readable_values('BasketballPositionType') %}
2727
{{ key }} => {{ value }}<br />
2828
{% endfor %}
@@ -42,8 +42,10 @@ C => Center
4242
### More features
4343

4444
* [NULL values](./null_values.md "NULL values")
45+
* [Default value](./default_value.md "Default value")
4546
* [Building the form](./building_the_form.md "Building the form")
4647
* [Additional methods](./additional_methods.md "Additional methods")
48+
* [Common types](./common_types.md "Common types")
4749
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
4850
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
4951
* [Hook for Doctrine migrations](./hook_for_doctrine_migrations.md "Hook for Doctrine migrations")

Resources/docs/example_of_using.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In this example will be shown how to create a custom ENUM field for basketball p
88
* `PF` - Power Forward
99
* `C` - Center
1010

11-
Create a class for new ENUM type `BasketballPositionType`:
11+
Create a class for a new ENUM type `BasketballPositionType`:
1212

1313
```php
1414
<?php
@@ -34,9 +34,9 @@ final class BasketballPositionType extends AbstractEnumType
3434
}
3535
```
3636

37-
Register `BasketballPositionType` for Doctrine in config.yml:
37+
Register `BasketballPositionType` for Doctrine in config.yaml:
3838

39-
```yml
39+
```yaml
4040
doctrine:
4141
dbal:
4242
types:
@@ -109,8 +109,10 @@ use App\DBAL\Types\BasketballPositionType;
109109
### More features
110110

111111
* [NULL values](./null_values.md "NULL values")
112+
* [Default value](./default_value.md "Default value")
112113
* [Building the form](./building_the_form.md "Building the form")
113114
* [Additional methods](./additional_methods.md "Additional methods")
115+
* [Common types](./common_types.md "Common types")
114116
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
115117
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
116118
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")

Resources/docs/hook_for_doctrine_migrations.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ You should repeat these steps after each update of your custom ENUM type!
2626
### More features
2727

2828
* [NULL values](./null_values.md "NULL values")
29+
* [Default value](./default_value.md "Default value")
2930
* [Building the form](./building_the_form.md "Building the form")
3031
* [Additional methods](./additional_methods.md "Additional methods")
32+
* [Common types](./common_types.md "Common types")
3133
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
3234
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
3335
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")

Resources/docs/null_values.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
```php
66
/** @ORM\Column(name="position", type="BasketballPositionType", nullable=true) */
7-
protected $position;
7+
private $position;
88

99
// or
1010

1111
/** @ORM\Column(name="position", type="BasketballPositionType", nullable=false) */
12-
protected $position;
12+
private $position;
1313
```
1414

1515
---
1616

1717
### More features
1818

19+
* [Default value](./default_value.md "Default value")
1920
* [Building the form](./building_the_form.md "Building the form")
2021
* [Additional methods](./additional_methods.md "Additional methods")
22+
* [Common types](./common_types.md "Common types")
2123
* [Readable ENUM values in templates](./readable_enum_values_in_template.md "Readable ENUM values in templates")
2224
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
2325
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")

Resources/docs/readable_enum_values_in_template.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You might want to show ENUM values rendered in your templates in *readable forma
44
It is easy to do by using the custom TWIG filter `|readable_enum` that was implemented for this purpose.
55
In the example below if Player is a Point Guard in their basketball team then position will be rendered in template as `Point Guard` instead of `PG`.
66

7-
```jinja
7+
```twig
88
{{ player.position|readable_enum }}
99
```
1010

@@ -15,7 +15,7 @@ If only one ENUM type found, then it is possible to get the readable value from
1515
For example `BasketballPositionType` and `MapLocationType` can have same ENUM value `C` with its readable variant `Center`.
1616
The code below will throw an exception, because without additional parameter for `|readable_enum` filter, it can't determine which ENUM type to use in which case:
1717

18-
```jinja
18+
```twig
1919
{{ set player_position = 'C' }}
2020
{{ set location_on_the_map = 'C' }}
2121
@@ -25,7 +25,7 @@ The code below will throw an exception, because without additional parameter for
2525

2626
So, the correct usage of `|readable_enum` filter in this case should be with additional parameter, that specifies the ENUM type:
2727

28-
```jinja
28+
```twig
2929
{{ set player_position = 'C' }}
3030
{{ set location_on_the_map = 'C' }}
3131
@@ -38,8 +38,10 @@ So, the correct usage of `|readable_enum` filter in this case should be with add
3838
### More features
3939

4040
* [NULL values](./null_values.md "NULL values")
41+
* [Default value](./default_value.md "Default value")
4142
* [Building the form](./building_the_form.md "Building the form")
4243
* [Additional methods](./additional_methods.md "Additional methods")
44+
4345
* [ENUM constants in templates](./enum_constants_in_templates.md "ENUM constants in templates")
4446
* [ENUM values in templates](./enum_values_in_templates.md "ENUM values in templates")
4547
* [Hook for Doctrine migrations](./hook_for_doctrine_migrations.md "Hook for Doctrine migrations")

0 commit comments

Comments
 (0)