Skip to content

Commit 34a1f40

Browse files
Finishing the Types class
1 parent d48113b commit 34a1f40

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ Enhances Doctrine with PostgreSQL-specific features and functions. Supports Post
99

1010
## Quick Start
1111

12+
1213
```php
14+
use MartinGeorgiev\Doctrine\DBAL\Type;
15+
1316
// Register types with Doctrine
14-
Type::addType('jsonb', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\Jsonb");
15-
Type::addType('text[]', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\TextArray");
16-
Type::addType('numrange', "MartinGeorgiev\\Doctrine\\DBAL\\Types\\NumRange");
17+
Type::addType(Type::JSONB, "MartinGeorgiev\\Doctrine\\DBAL\\Types\\Jsonb");
18+
Type::addType(Type::TEXT_ARRAY, "MartinGeorgiev\\Doctrine\\DBAL\\Types\\TextArray");
19+
Type::addType(Type::NUMRANGE, "MartinGeorgiev\\Doctrine\\DBAL\\Types\\NumRange");
1720

1821
// Use in your Doctrine entities
19-
#[ORM\Column(type: 'jsonb')]
22+
#[ORM\Column(type: Type::JSONB)]
2023
private array $data;
2124

22-
#[ORM\Column(type: 'text[]')]
25+
#[ORM\Column(type: Type::TEXT_ARRAY)]
2326
private array $tags;
2427

25-
#[ORM\Column(type: 'numrange')]
28+
#[ORM\Column(type: Type::NUMRANGE)]
2629
private NumericRange $priceRange;
2730

2831
// Use in DQL

docs/INTEGRATING-WITH-SYMFONY.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Once configured, you can use the PostgreSQL types in your Symfony entities:
301301
namespace App\Entity;
302302
303303
use Doctrine\ORM\Mapping as ORM;
304+
use MartinGeorgiev\Doctrine\DBAL\Type;
304305
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\DateRange;
305306
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Ltree;
306307
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\NumericRange;
@@ -316,25 +317,25 @@ class Product
316317
#[ORM\Column]
317318
private ?int $id = null;
318319
319-
#[ORM\Column(type: 'jsonb')]
320+
#[ORM\Column(type: Type::JSONB)]
320321
private array $specifications = [];
321322
322-
#[ORM\Column(type: 'text[]')]
323+
#[ORM\Column(type: Type::TEXT_ARRAY)]
323324
private array $categories = [];
324325
325-
#[ORM\Column(type: 'point')]
326+
#[ORM\Column(type: Type::POINT)]
326327
private Point $manufacturingLocation;
327328
328-
#[ORM\Column(type: 'numrange')]
329+
#[ORM\Column(type: Type::NUMRANGE)]
329330
private NumericRange $priceRange;
330331
331-
#[ORM\Column(type: 'daterange')]
332+
#[ORM\Column(type: Type::DATERANGE)]
332333
private DateRange $availabilityPeriod;
333334
334-
#[ORM\Column(type: 'inet')]
335+
#[ORM\Column(type: Type::INET)]
335336
private string $originServerIp;
336337
337-
#[ORM\Column(type: 'ltree')]
338+
#[ORM\Column(type: Type::LTREE)]
338339
private Ltree $pathFromRoot;
339340
}
340341
```
@@ -377,15 +378,17 @@ If you need to register types programmatically (e.g., in a bundle), you can do s
377378
namespace App\Service;
378379
379380
use Doctrine\DBAL\Types\Type;
381+
use MartinGeorgiev\Doctrine\DBAL\Type;
382+
use MartinGeorgiev\Doctrine\DBAL\Types\Jsonb;
380383
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
381384
382385
#[Autoconfigure(lazy: true)]
383386
class DoctrineTypeRegistrar
384387
{
385388
public function registerTypes(): void
386389
{
387-
if (!Type::hasType('jsonb')) {
388-
Type::addType('jsonb', \MartinGeorgiev\Doctrine\DBAL\Types\Jsonb::class);
390+
if (!Type::hasType(Type::JSONB)) {
391+
Type::addType(Type::JSONB, Jsonb::class);
389392
}
390393
391394
// Register other types as needed...

src/MartinGeorgiev/Doctrine/DBAL/Type.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,39 @@
44

55
namespace MartinGeorgiev\Doctrine\DBAL;
66

7-
enum Type
7+
final class Type
88
{
9+
// Array Types
910
public const BIGINT_ARRAY = 'bigint[]';
11+
public const BOOLEAN_ARRAY = 'bool[]';
12+
public const CIDR_ARRAY = 'cidr[]';
13+
public const DOUBLE_PRECISION_ARRAY = 'double precision[]';
14+
public const GEOGRAPHY_ARRAY = 'geography[]';
15+
public const GEOMETRY_ARRAY = 'geometry[]';
16+
public const INET_ARRAY = 'inet[]';
17+
public const INTEGER_ARRAY = 'integer[]';
18+
public const JSONB_ARRAY = 'jsonb[]';
19+
public const MACADDR_ARRAY = 'macaddr[]';
20+
public const POINT_ARRAY = 'point[]';
21+
public const REAL_ARRAY = 'real[]';
22+
public const SMALLINT_ARRAY = 'smallint[]';
23+
public const TEXT_ARRAY = 'text[]';
24+
25+
// Scalar Types
26+
public const CIDR = 'cidr';
27+
public const GEOGRAPHY = 'geography';
28+
public const GEOMETRY = 'geometry';
29+
public const INET = 'inet';
30+
public const JSONB = 'jsonb';
31+
public const LTREE = 'ltree';
32+
public const MACADDR = 'macaddr';
33+
public const POINT = 'point';
34+
35+
// Range Types
36+
public const DATERANGE = 'daterange';
37+
public const INT4RANGE = 'int4range';
38+
public const INT8RANGE = 'int8range';
39+
public const NUMRANGE = 'numrange';
40+
public const TSRANGE = 'tsrange';
41+
public const TSTZRANGE = 'tstzrange';
1042
}

0 commit comments

Comments
 (0)