title: Map array to entity in Symfony2/Doctrine2
body: <p>I'm using the DoctrineFixtures bundle to create example entities during development. In my ORM fixtures load() method, I define the data as associative arrays and create the entity object in a loop.</p>
  
  <pre><code>&lt;?php
  // ...
  public function load($manager) {
      $roleDefs = array(
          'role-1' =&gt; array(
               'role' =&gt; 'administrator'
          ),
          'role-2' =&gt; array(
               'role' =&gt; 'user'
          ),
      );
  
      foreach($roleDefs as $key =&gt; $roleDef) {
          $role = new Role();
          $role-&gt;setRole($roleDef['role']);
          $manager-&gt;persist($role);
  
          $this-&gt;addReference($key, $role);
      }
  
      $manager-&gt;flush();
  }
  </code></pre>
  
  <p>I always use the same array schema. Every array element uses the property name (in underscore notation) of the entity as index. If the entity structure becomes more complex, there are a lot of <code>$entity-&gt;setMyProperty($def['my_property']);</code> lines.</p>
  
  <p>I think the problem of mapping propertynames to setter methods is a very common problem in Symfony and Doctrine as this type of mapping is found in many situations (e.g. mapping forms to entities).</p>
  
  <p>Now I'm wondering if there is a built-in method that can be used for mapping. It would be nice to have a solution like</p>
  
  <pre><code>foreach($defs as $key =&gt; $def) {
     $entity = $magicMapper-&gt;getEntity('MyBundle:MyEntity', $def);
     // ...
  }
  </code></pre>
  
  <p>Has someone an idea how this can be achieved?</p>
  
  <p>Thanks a lot,
  Hacksteak</p>