Symfony2 DTO, Entity conversion Symfony2 DTO, Entity conversion symfony symfony

Symfony2 DTO, Entity conversion


Start by using public properties on your dto's. That eliminates a bunch of getter/setter methods which really should not do anything for dto's. You can always add some majic methods for special cases.

Next, rethink the design of your DoctrineUserEntity aka Domain object. Do you really need getter/setter for each attribute? If so then what's the point?

Instead try to group properties into value objects:

$userNameValueObject = new UserNameValueObject($userDto->firstName, $userDto->lastName);$userEntity = new UserEntity($userDTO->username,$userDTO->password, $userNameValueObject);// And maybe this for updates$userEntity->updateName($userNameValueObject);

But again, make sure you are actually getting some value for your work. A bunch of one to one mappings might make sense on other platforms where domain objects can stay alive between request. In php, everything starts from ground zero.


One option I've recently found is https://github.com/jasonrobertfox/DTOx which is a generator for DTO's and tests. It does the annoying boiler plate generation work for you.