Skip to content

Commit cf1f71b

Browse files
authored
chore(docs): update docs with recent changes (#291)
2 parents 790f78b + 4ee0f1b commit cf1f71b

File tree

8 files changed

+151
-7
lines changed

8 files changed

+151
-7
lines changed

docs/_nav.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Configuration](getting-started/configuration.md)
66
- [Cache](getting-started/cache.md)
77
- [Mapping](mapping/index.md)
8+
- [Mapper attribute](mapping/mapper-attribute.md)
89
- [MapTo and MapFrom attributes](mapping/attributes.md)
910
- [Symfony Serializer](mapping/serializer.md)
1011
- [Mapping Collections](mapping/map-collection.md)
@@ -14,6 +15,7 @@
1415
- [Transformer](mapping/transformer.md)
1516
- [Provider](mapping/provider.md)
1617
- [Mapping inheritance](mapping/inheritance.md)
18+
- [Identifier: mapping existing objects](mapping/identifier.md)
1719
- [DateTime format](mapping/date-time.md)
1820
- [Symfony Bundle](bundle/index.md)
1921
- [Installation](bundle/installation.md)

docs/bundle/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ indicate if we use the attribute of the symfony/serializer during the mapping, t
7979
`#[MaxDepth]`, `#[Ignore]` and `#[DiscriminatorMap]` attributes;
8080
* `api_platform` (default: `false`): A boolean which indicate if we use services from the api-platform/core package and
8181
inject extra data (json ld) in the mappers when we map a Resource class to or from an array.
82+
* `doctrine` (default: `false`): A boolean which indicate if we want to use service from doctrine to extract more
83+
information about the classes, and use doctrine to fetch existing objects. It will use by default the `doctrine.orm.entity_manager`
84+
service if present, will then try to use the `doctrine_mongodb.odm.document_manager` service, or throw an exception if none of them
85+
are present. You can specify your own manager by overriding the `automapper.doctrine.object_manager` service.
8286
* `name_converter` (default: `null`): A service id which implement the `AdvancedNameConverterInterface` or
8387
`NameConverterInterface` (starting from 7.2) from the symfony/serializer, this name converter will be used when
8488
mapping from an array to an object and vice versa;

docs/getting-started/configuration.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ $configuration = new Configuration(
1616
autoRegister: true,
1717
mapPrivateProperties: true,
1818
allowReadonlyTargetToPopulate: false,
19+
strictTypes: false,
20+
reloadStrategy: \AutoMapper\Loader\FileReloadStrategy::ON_CHANGE,
21+
allowExtraProperties: false,
22+
extractTypesFromGetter: false,
1923
);
2024
$autoMapper = AutoMapper::create(configuration: $configuration);
2125
```
@@ -58,7 +62,32 @@ Setting this to false will make the `AutoMapper` throw an exception if the mappe
5862
This can be useful if you want to pre generate all the mappers and have tests to ensure that all the mappers are
5963
generated.
6064

65+
* `mapPrivateProperties` (default: `true`)
66+
67+
When set to true, the generated mappers will map private and protected properties.
68+
69+
* `strictTypes` (default: `false`)
70+
71+
When set to true, the generated mappers will add `declare(strict_types=1);` at the top of the generated files.
72+
73+
* `reloadStrategy` (default: `FileReloadStrategy::ON_CHANGE`)
74+
75+
Allow specifying which strategy to use to reload the generated mappers. It can be one of the following:
76+
77+
* `FileReloadStrategy::NEVER`: never reload the mapper, even if the source code changes. This is useful in production
78+
environments where you are sure that the source code will not change and avoid useless checks.
79+
* `FileReloadStrategy::ON_CHANGE`: reload the mapper only if the source code changes. This is a good compromise between
80+
performance and development convenience.
81+
* `FileReloadStrategy::ALWAYS`: reload the mapper each time it is needed. This is useful when debugging automapper itself, but
82+
it is not recommended for most cases as it will slow down the performance.
83+
6184
* `allowExtraProperties` (default: `false`)
6285

6386
Settings this to true will allow the mapper to map extra properties from the source object to the target object when
6487
the source or the target implements the `ArrayAccess` interface.
88+
89+
* `extractTypesFromGetter` (default: `false`)
90+
91+
When set to true, the generated mappers will used the return type of the getters to determine the type of the property
92+
even when writing to the property. This can be useful with covariance when the getter return a more specific type than the
93+
setter.

docs/mapping/identifier.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Identifier : Mapping existing objects
2+
3+
With AutoMapper you can can map data to an existing object.
4+
5+
```php
6+
// We fetch existing object from database
7+
$user = $this->entityManager->find(User::class, $id);
8+
9+
// We map data to existing object
10+
$mapper->map($requestValue, $user);
11+
```
12+
13+
This works fine for most cases, but you may have nested objects in your source and destination objects.
14+
15+
As an example, let's say your `User` object has multiple `Address` object.
16+
17+
```php
18+
class User
19+
{
20+
public string $name;
21+
/** @var Address[] */
22+
public array $address;
23+
}
24+
25+
class Address
26+
{
27+
public string $id;
28+
public string $street;
29+
public string $city;
30+
}
31+
```
32+
33+
When mapping data to an existing `User` object, you don't want to instantiate new `Address` objects as it may
34+
create duplicates in your database or even worse fail with an exception because it's already existing.
35+
36+
For that purpose, AutoMapper support the concept of Identifier which allow to reuse existing objects in a collection base
37+
on their identifiers.
38+
39+
When AutoMapper is configured to be used with an ORM like Doctrine, it will automatically use the metadata to find the identifier of your objects.
40+
41+
In other cases, you can manually tell AutoMapper which property to use as identifier.
42+
43+
```php
44+
class Address
45+
{
46+
#[MapFrom(identifier: true)]
47+
public string $id;
48+
}
49+
```
50+
51+
When mapping a collection of `Address` objects, AutoMapper will now look for existing objects in the destination collection and use
52+
them if identifiers match.
53+
54+
Otherwise, it will create a new object and add it to the collection.

docs/mapping/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
Despite doing its best to map objects automatically, AutoMapper provides a lot of ways to customize the mapping between
44
a `source` and a `target`.
55

6+
- [Mapper attribute](mapper-attribute.md)
67
- [MapTo and MapFrom attributes](attributes.md)
78
- [Symfony Serializer attributes](serializer.md)
9+
- [Mapping a collection](map-collection.md)
810
- [Ignoring properties](ignoring-properties.md)
911
- [Conditional mapping](conditional-mapping.md)
1012
- [Groups](groups.md)
1113
- [Transformer](transformer.md)
1214
- [Provider](provider.md)
1315
- [Mapping inheritance](inheritance.md)
16+
- [Identifier: mapping existing objects](identifier.md)
1417
- [DateTime format](date-time.md)

docs/mapping/mapper-attribute.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ You can also limit the scope of the `#[Mapper]` attribute to a specific source o
2020
#[Mapper(source: EntityDto::class, constructorStrategy: ConstructorStrategy::NEVER)]
2121
```
2222

23-
## DateTime format
24-
25-
You can override DateTime format for a whole class by using `#[Mapper]` attribute:
23+
The `source` or `target` parameters can also be an array of classes, to override the mapping for multiple sources or targets
24+
with a single attribute.
2625

2726
```php
28-
#[Mapper(dateTimeFormat: \DateTimeInterface::ATOM)]
27+
#[Mapper(source: [EntityDto::class, AnotherEntityDto::class], constructorStrategy: ConstructorStrategy::NEVER)]
2928
```
3029

31-
This way, all your class properties that are DateTime will be transformed to string with the corresponding format.
32-
For more details about how each DateTime format configuration works together please read the dedicated page:
33-
[DateTime format](./date-time.md).
30+
## Configuration
31+
32+
The `#[Mapper]` attribute supports most of the configuration parameters specified in the [global configuration](../getting-started/configuration.md).
33+
It will override the global configuration for the specified mapping.
34+
35+
## Register
36+
37+
This attribute may also be used when registering mappers manually when using the Symfony bundle.
3438

3539
## Priority
3640

docs/mapping/provider.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,39 @@ class MyProvider implements ProviderInterface
7474
}
7575
}
7676
```
77+
78+
## Third party providers
79+
80+
We also provide some third party providers that allow you to use popular libraries to instantiate the `target` object :
81+
82+
### Doctrine
83+
84+
Automatically fetch the target from the database if it's an entity.
85+
86+
This provider is automatically registered when :
87+
88+
* an `Doctrine\Persistence\ObjectManager` is given when creating the `AutoMapper` object or is configured in the Symfony Bundle.
89+
* The `target` class is managed by this `ObjectManager`.
90+
91+
### Api Platform
92+
93+
Use the Api Platform Provider to fetch the target object. It also converts IRIs to objects.
94+
95+
This provider is automatically registered when :
96+
97+
* The Api Platform library is installed and `api_platform` is enabled in the Symfony Bundle Configuration.
98+
* The `target` class is a resource managed by Api Platform.
99+
100+
### Disable auto registration
101+
102+
If you want to disable the automatic registration of the Doctrine and Api Platform providers on a specific Mapping,
103+
you can use the `#[MapProvider]` attribute with `false`.
104+
105+
```php
106+
use AutoMapper\Attribute\MapProvider;
107+
108+
#[MapProvider(provider: false)]
109+
class Entity
110+
{
111+
}
112+
```

docs/mapping/serializer.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter
6868
$autoMapper = AutoMapper::create(nameConverter: new CamelCaseToSnakeCaseNameConverter());
6969
```
7070

71+
It also supports the `Symfony\Component\Serializer\Attribute\SerializedName` attribute.
72+
73+
```php
74+
use Symfony\Component\Serializer\Attribute\SerializedName;
75+
76+
class Source
77+
{
78+
#[SerializedName('nested')]
79+
public $nestedProperty;
80+
}
81+
```
82+
7183
[More information on the Name converters](https://symfony.com/doc/current/components/serializer.html#converting-property-names-when-serializing-and-deserializing)
7284

7385
### Normalizer Bridge

0 commit comments

Comments
 (0)