Skip to content

Commit 22c98eb

Browse files
committed
add enums simple docs
1 parent f201a2f commit 22c98eb

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
PostgreSQL Doctrine
22
==============
33

4+
[![Latest Stable Version](http://poser.pugx.org/pfilsx/postgresql-doctrine/v)](https://packagist.org/packages/pfilsx/postgresql-doctrine)
5+
[![PHP Version Require](http://poser.pugx.org/pfilsx/postgresql-doctrine/require/php)](https://packagist.org/packages/pfilsx/postgresql-doctrine)
6+
[![Total Downloads](http://poser.pugx.org/pfilsx/postgresql-doctrine/downloads)](https://packagist.org/packages/pfilsx/postgresql-doctrine)
7+
48
Description
59
------------
610

711
Provides extended Doctrine DBAL and Doctrine migration classes to allow you to use PostgreSQL
8-
specific features such as [enums](https://www.postgresql.org/docs/current/datatype-enum.html) with Doctrine.
12+
specific features such as [enums](https://www.postgresql.org/docs/current/datatype-enum.html) or JSON(B) with Doctrine.
913

1014
Features
1115
--------
@@ -36,4 +40,20 @@ Usage
3640

3741
Please refer [Doctrine DBAL](https://www.doctrine-project.org/projects/doctrine-dbal/en/current/index.html)
3842
and [Doctrine Migrations](https://www.doctrine-project.org/projects/doctrine-migrations/en/3.5/index.html)
39-
for instructions on how to override the default doctrine classes in your project.
43+
for instructions on how to override the default doctrine classes in your project.
44+
45+
Required steps:
46+
1. Register [PostgreSQLDriverMiddleware.php](src/DBAL/Middleware/PostgreSQLDriverMiddleware.php) as driver middleware
47+
2. Register [OrmSchemaProvider.php](src/Migrations/Provider/OrmSchemaProvider.php) as Doctrine\Migrations\Provider\SchemaProvider in Doctrine\Migrations\DependencyFactory
48+
49+
For Symfony integration see [PostgreSQLDoctrineBundle](https://github.com/pfilsx/PostgreSQLDoctrineBundle)
50+
51+
Documentation
52+
-------------
53+
54+
* [ENUMS](docs/ENUMS.md)
55+
56+
License
57+
-------
58+
59+
This bundle is released under the MIT license.

docs/ENUMS.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Overview
2+
========
3+
4+
This package provides column type and extended DBAL classes for [PostgreSQL enums](https://www.postgresql.org/docs/current/datatype-enum.html) support for Doctrine and Doctrine migrations.
5+
Column type is based on enumType option of Column attribute/annotation.
6+
7+
Example
8+
--------
9+
10+
```php
11+
12+
enum ExampleEnum: string
13+
{
14+
case Case1 = 'case_1';
15+
case Case2 = 'case_2';
16+
}
17+
18+
#[Entity]
19+
class Example
20+
{
21+
/** ... */
22+
23+
#[Column(type: 'enum', enumType: ExampleEnum::class)]
24+
public ExampleEnum $type;
25+
}
26+
```
27+
28+
For the example above the doctrine migrations will generate migration with PostgreSQL enum type creation and column uses this type
29+
```php
30+
public function up(Schema $schema): void
31+
{
32+
$this->addSql('CREATE TYPE example_enum_type AS ENUM(\'case_1\', \'case_2\')');
33+
$this->addSql('COMMENT ON TYPE example_enum_type IS \'ExampleEnum\'');
34+
35+
$this->addSql('CREATE TABLE example (..., type example_enum_type NOT NULL, ...)');
36+
37+
/** ... */
38+
}
39+
40+
public function down(Schema $schema): void
41+
{
42+
/** ... */
43+
44+
$this->addSql('DROP TABLE example');
45+
$this->addSql('DROP TYPE example_enum_type');
46+
}
47+
```
48+
49+
50+
Supported actions in migrations
51+
-----------------
52+
53+
1. Create new enums based on usages in entities
54+
2. Remove enums based on usages in entities
55+
3. Update existing enum by adding new labels
56+
4. Recreate existing enum for removing labels
57+
58+
:warning: Attention :warning:
59+
60+
Adding/removing labels to existing enums will generate SQL which may fail on execution if your tables use those labels.
61+
Be careful and control your data!

0 commit comments

Comments
 (0)