All utilities are accessible from the DtoUtility
. Some may require to inject the class as a service, others are accessible whatever the context via static functions.
Doctrine supports badly the changes in an ArrayCollection
of entities in a OneToMany
or in ManyToMany
relationship. In facts, if a different instance of an ArrayCollection
replaces an existing one, Doctrine will remove all links and recreate the new, so it doesn't take into account the unchanged relations.
The function updateEntityList
updates an existing ArrayCollection
from an traversable object without replacing the instance of the input ArrayCollection
to avoid this bug.
It is possible to give in 3rd parameter the list of properties on which the comparison of the entities is made.
/**
* @param DummyEntity $dto
*
* @return self
*/
public function updateRelationship(DummyEntity $dto): self
DtoUtility::updateEntityList($this->relationship, $dto->newValues, ['id']);
return $this;
}
It can be useful to instanciate a DTO from an array out of the default controller argument conversion. For instance, you have an API call somewhere and you want to manipulate an object and validate the input data. Exactly what does the DataTransferObjectParamConverter
. Options can be passed to the ParamConverter aswell.
/**
* @return DummyDataTransferObject
*/
public makeAnApiCall(): DummyDataTransferObject
{
$response = $this->api->callSomething();
return $this->dtoUtility->loadArrayToDto(
$response->getContent(),
DummyDataTransferObject::class
);
}