|
| 1 | +# guikingone/symfony-uid-doctrine |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +The `guikingone/symfony-uid-doctrine` aim to provide the support of `Symfony/Uid` as a `Doctrine` type. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Consider using `Packagist` and `Composer` when installing the following project: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require guikingone/symfony-uid-doctrine |
| 14 | +``` |
| 15 | + |
| 16 | +## Configuration |
| 17 | + |
| 18 | +### Uuid |
| 19 | + |
| 20 | +```php |
| 21 | +Doctrine\DBAL\Types\Type::addType('uuid', Guikingone\SymfonyUid\Doctrine\Uuid\UuidType::class); |
| 22 | +``` |
| 23 | + |
| 24 | +```yaml |
| 25 | +# config/packages/doctrine.yaml |
| 26 | +doctrine: |
| 27 | + dbal: |
| 28 | + types: |
| 29 | + uuid: Guikingone\SymfonyUid\Doctrine\Uuid\UuidType |
| 30 | +``` |
| 31 | +
|
| 32 | +### Ulid |
| 33 | +
|
| 34 | +```php |
| 35 | +Doctrine\DBAL\Types\Type::addType('ulid', Guikingone\SymfonyUid\Doctrine\Ulid\UlidType::class); |
| 36 | +``` |
| 37 | + |
| 38 | +```yaml |
| 39 | +# config/packages/doctrine.yaml |
| 40 | +doctrine: |
| 41 | + dbal: |
| 42 | + types: |
| 43 | + ulid: Guikingone\SymfonyUid\Doctrine\Ulid\UlidType |
| 44 | +``` |
| 45 | +
|
| 46 | +## Usage |
| 47 | +
|
| 48 | +### Uuid |
| 49 | +
|
| 50 | +```php |
| 51 | +use Doctrine\ORM\Mapping as ORM; |
| 52 | +use Guikingone\SymfonyUid\Doctrine\Uuid\UuidGenerator; |
| 53 | + |
| 54 | +/** |
| 55 | + * @ORM\Entity |
| 56 | + * @ORM\Table(name="posts") |
| 57 | + */ |
| 58 | +class Post |
| 59 | +{ |
| 60 | + /** |
| 61 | + * @var Symfony\Component\Uid\Uuid |
| 62 | + * |
| 63 | + * @ORM\Id |
| 64 | + * @ORM\Column(type="uuid", unique=true) |
| 65 | + * @ORM\GeneratedValue(strategy="CUSTOM") |
| 66 | + * @ORM\CustomIdGenerator(class=UuidGenerator::class) |
| 67 | + */ |
| 68 | + protected $id; |
| 69 | + |
| 70 | + public function getId(): Uuid |
| 71 | + { |
| 72 | + return $this->id; |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +If you don't want to use the generator, consider using the `__construct()` method: |
| 78 | + |
| 79 | +```php |
| 80 | +use Doctrine\ORM\Mapping as ORM; |
| 81 | +use Symfony\Component\Uid\Uuid; |
| 82 | + |
| 83 | +/** |
| 84 | + * @ORM\Entity |
| 85 | + * @ORM\Table(name="posts") |
| 86 | + */ |
| 87 | +class Post |
| 88 | +{ |
| 89 | + /** |
| 90 | + * @var Symfony\Component\Uid\Uuid |
| 91 | + * |
| 92 | + * @ORM\Id |
| 93 | + * @ORM\Column(type="uuid", unique=true) |
| 94 | + */ |
| 95 | + protected $id; |
| 96 | + |
| 97 | + public function __construct() |
| 98 | + { |
| 99 | + $this->id = Uuid::v4(); |
| 100 | + } |
| 101 | + |
| 102 | + public function getId(): Uuid |
| 103 | + { |
| 104 | + return $this->id; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Ulid |
| 110 | + |
| 111 | +```php |
| 112 | +use Doctrine\ORM\Mapping as ORM; |
| 113 | +use Guikingone\SymfonyUid\Doctrine\Ulid\UlidGenerator; |
| 114 | + |
| 115 | +/** |
| 116 | + * @ORM\Entity |
| 117 | + * @ORM\Table(name="posts") |
| 118 | + */ |
| 119 | +class Post |
| 120 | +{ |
| 121 | + /** |
| 122 | + * @var Symfony\Component\Uid\Ulid |
| 123 | + * |
| 124 | + * @ORM\Id |
| 125 | + * @ORM\Column(type="ulid", unique=true) |
| 126 | + * @ORM\GeneratedValue(strategy="CUSTOM") |
| 127 | + * @ORM\CustomIdGenerator(class=UlidGenerator::class) |
| 128 | + */ |
| 129 | + protected $id; |
| 130 | + |
| 131 | + public function getId(): Ulid |
| 132 | + { |
| 133 | + return $this->id; |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +If you don't want to use the generator, consider using the `__construct()` method: |
| 139 | + |
| 140 | +```php |
| 141 | +use Doctrine\ORM\Mapping as ORM; |
| 142 | +use Symfony\Component\Uid\Ulid; |
| 143 | + |
| 144 | +/** |
| 145 | + * @ORM\Entity |
| 146 | + * @ORM\Table(name="posts") |
| 147 | + */ |
| 148 | +class Post |
| 149 | +{ |
| 150 | + /** |
| 151 | + * @var Symfony\Component\Uid\Ulid |
| 152 | + * |
| 153 | + * @ORM\Id |
| 154 | + * @ORM\Column(type="ulid", unique=true) |
| 155 | + */ |
| 156 | + protected $id; |
| 157 | + |
| 158 | + public function __construct() |
| 159 | + { |
| 160 | + $this->id = new Ulid(); |
| 161 | + } |
| 162 | + |
| 163 | + public function getId(): Ulid |
| 164 | + { |
| 165 | + return $this->id; |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## Contributing |
| 171 | + |
| 172 | +Contributions are welcome! Please read [CONTRIBUTING](.github/CONTRIBUTING.md) for details. |
0 commit comments