title: Symfony2 DTO, Entity conversion
body: <p>I'm creating symfony2 application with doctrine2 and I would like to ask for advice regarding common/good practice for DTO-Entity, Entity-DTO conversion. I've found some information for all languages and frameworks, but none for SF2.</p>
  
  <p>I would like to isolate Entities, so they are used only in Services and DAO's (Managers, Repositories in SF2 terminology). Controllers won't ever see DAO's or Entities and will interact with business logic only via Services. All communication between Services and Controllers should be done via primitive types, scalars, DTO's.</p>
  
  <p>Example :</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>class RegistrationController extends Controller
  {
      public function registerAction($name)
      {
          $userDTO = new UserDTO();
          $form = $this-&gt;createForm(new UserType(), $userDTO);
  
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
              $userService = $this-&gt;get('userService');
  
              $userService-&gt;createUser($userDTO);
  
              return $this-&gt;redirect($this-&gt;generateUrl('success'));
          }
  
      --//--
      }
  }
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>class UserServiceImpl implements UserService
  {
  
      private $userDao;
  
      public function __construct(UserDao $userDao)
      {
          $this-&gt;userDao = $userDao;
      }
  
      public function createUser(UserDTO $user)
      {
          $user = new User(); #doctrine entity
          $user-&gt;setFirstName($userDTO-&gt;getFirstName());
          $user-&gt;setLastName($userDTO-&gt;getLastName());
  
          $this-&gt;userDao-&gt;persist($user);
          $this-&gt;userDao-&gt;flush();
  
          --//--
      }
  }
  </code></pre>
  
  <p>Problem quickly appears with rising amount of properties in User object. In my application User has 13 fields. Are there any SF2 tools (classes) to simplify this process ? Do you write your own convertors / transformers ? Could you please show example of how it should look like ? Maby PHP magic methods could help ? What about reflection ?</p>
  
  <p>Thanks for advices and opinions.</p>