-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to map an array of dto #218
Comments
Hi, Finally, after analyzing the ArrayTransformer, I found a solution consisting in adding a method add and a method remove to the dto. Probably because of this if/else in AbstractArrayTransformer that check if type of writeMutator is TYPE_ADDER_AND_REMOVER (5), if i dont set add and remove method, i got a type TYPE_PROPERTY (2) if ($propertyMapping->target->writeMutator && $propertyMapping->target->writeMutator->type === WriteMutator::TYPE_ADDER_AND_REMOVER) {
/**
* Use add and remove methods.
*
* $target->add($output);
*/
$mappedValueVar = new Expr\Variable($uniqueVariableScope->getUniqueName('mappedValue'));
$itemStatements[] = new Stmt\Expression(new Expr\Assign($mappedValueVar, $output));
$itemStatements[] = new Stmt\If_(new Expr\BinaryOp\NotIdentical(new Expr\ConstFetch(new Name('null')), $mappedValueVar), [
'stmts' => [
new Stmt\Expression($propertyMapping->target->writeMutator->getExpression($target, $mappedValueVar, $assignByRef)),
],
]);
// dd($input, $target, $uniqueVariableScope, $propertyMapping, $source);
} else {
/*
* Assign the value to the array.
*
* $values[] = $output;
* or
* $values[$key] = $output;
*/
// dd($output);
// dd(new Stmt\Expression($this->getAssignExpr($valuesVar, $output, $loopKeyVar, $assignByRef)));
$itemStatements[] = new Stmt\Expression($this->getAssignExpr($valuesVar, $output, $loopKeyVar, $assignByRef));
} So adding a method addJob(JobDto $dto) and removeJob(JobDto $dto) transforms the array of Entity in array of dtos. public function addJob(JobDto $dto): self
{
$this->jobs[] = $dto;
return $this;
}
public function removeJob(CollaboratorDto $dto): self
{
foreach ($this->jobs as $index => $job) {
if ($job === $dto) {
unset($this->jobs[$index]);
}
}
return $this;
} Is this normal behavior? I find it odd to have to create these methods for an array especially in a dto object |
I also had a problem with mapping source Collection property to target array property. Not sure what is the best approach for this. Docs are not covering this issue. |
Hi,
I'm actually using your mapper in a symfony app with api platform. I'm trying to use mapper in a custom EntityToDtoProvider to automatically hydrate dtos from entities.
It's almost working, but it seems like array of objects are not transformed into dto.
Dto is something like that :
Entity is something like that
Custom provider is like that
But when i make a call through the api, i got a CompanyDto but the propety jobs in dto is expected to have an array of JobDto but i got an array of Job Entity.
What i'm missing ? Do I need to add something to get this behavior ?
Thanks a lot
The text was updated successfully, but these errors were encountered: