Skip to content

Commit

Permalink
Add documentation for #[DoctrineType] attribute usage
Browse files Browse the repository at this point in the history
  • Loading branch information
benr77 committed May 25, 2024
1 parent e547b23 commit 733b8f2
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,68 @@ return [

## Features

- [Dynamically Create Types For Standard Objects](#dynamically-create-types-for-standard-objects)
- [Auto-Register Custom Doctrine Type Mappings](#auto-register-custom-doctrine-type-mappings)
- [Auto-Register Carbon Doctrine Type Mappings](#auto-register-carbon-datetime-type-mappings)

### Dynamically Create Types For Standard Objects

Applying the `#[DoctrineType]` attribute to a class will generate a Doctrine mapping type for the object and register
it in the application. To configure this behaviour, you must specify where to look for the classes to auto-register.

```yaml
headsnet_doctrine_tools:
root_namespace: Headsnet\DoctrineToolsBundle
custom_types:
scan_dirs:
- 'src/' # the default value
```
For example, we have a string backed value object:
```php
namespace App\Domain\Model;

use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType;

#[DoctrineType(name: 'person_name', type: 'string')]
class PersonName
{
public function __construct(
private readonly string $value
) {
}

public static function create(string $value): self
{
return new self($value);
}

public function asString(): string
{
return $this->value;
}
}
```

This will cause a PHP file to be generated that provides the Doctrine Mapping Type for the class - something like:

```php
class PersonNameType extends \Headsnet\DoctrineToolsBundle\Types\StandardTypes\AbstractStringMappingType {
public function getName(): string
{
return 'dummy_string';
}

public function getClass(): string
{
return 'App\Domain\Model\PersonName';
}
}
```

This file is created in `src/_generated/` - you will want to add this directory to your `.gitignore` file.

### Auto-Register Custom Doctrine Type Mappings

The bundle can auto-register custom Doctrine DBAL types, eliminating the need to specify them all in
Expand All @@ -48,6 +107,7 @@ Then add the `#[DoctrineTypeMapping]` attribute to the custom type class:

```php
use Doctrine\DBAL\Types\Type;
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineMappingType;
#[DoctrineTypeMapping]
final class ReservationIdType extends Type
Expand Down

0 comments on commit 733b8f2

Please sign in to comment.