Skip to content
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

Setter does not get called if the property is also in the constructor #57

Closed
rsavuliak-macpaw opened this issue Apr 30, 2024 · 1 comment · Fixed by #58
Closed

Setter does not get called if the property is also in the constructor #57

rsavuliak-macpaw opened this issue Apr 30, 2024 · 1 comment · Fixed by #58

Comments

@rsavuliak-macpaw
Copy link

So the issue for me is - when you are trying to map onto an existing entity, which has constructor and setters - mapping does not work.

Here is an example:

class ParentObjDto
{
    public string $name;

    public ChildObjDto $child;
}
class ChildObjDto
{
    public string $a;
}
class ParentObj
{
    public function __construct(
        private string $name,
        private ChildObj $child,
    ) {
    }

    public function getChild(): ChildObj
    {
        return $this->child;
    }

    public function setChild(ChildObj $child): void
    {
        $this->child = $child;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}
class ChildObj
{
    public function __construct(
        private string $a,
    ) {
    }

    public function getA(): string
    {
        return $this->a;
    }

    public function setA(string $a): void
    {
        $this->a = $a;
    }
}

When you do mapping with a new object, passing target object class:

$dto = new ParentObjDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjDto();
$dto->child->a = 'dto-a';

$entity = $this->map($dto, ParentObj::class);

... you get a perfect result:

 App\ParentObj {#1358
  -name: "dto-name"
  -child: 
  App\ChildObj {#1362
      -a: "dto-a"
  }
}

However, when you try to map onto existing object, let's say, doctrine entity fetched from DB:

$dto = new ParentObjDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjDto();
$dto->child->a = 'dto-a';

$entity =  new ParentObj('entity-name', new ChildObj('entity-a'));
$entity = $this->map($dto, $entity);

... you properties are not changed

 App\ParentObj {#1258
  -name: "entity-name"
  -child: 
  App\ChildObj {#1362
      -a: "entity-a"
  }
}

Dunno, if it should be like that - but with the existence of setters, I would expect them to be used, cause you already have initialized object - no need for constructor. To make it work I need to remove properties from constructor.

@priyadi priyadi changed the title Mapping onto existing objects with constructor Setter does not get called if the property is also in the constructor Apr 30, 2024
@priyadi
Copy link
Member

priyadi commented May 1, 2024

The fix has been merged to #main. Thank you for your bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants