title: Table joining using zend2 hydrate
body: <p>I have a form to insert users to DB, which needs to update two tables @ once, <strong>user</strong> table and <strong>user_role_linker</strong> table. I am using hydrator (ClassMethod) to map my entities with the relative tables. I use two seperate entities, <strong>User</strong> and <strong>UserRole</strong>. The form contains all user fields that map with <strong>User</strong> table and user role field to map with <strong>user_role_linker</strong> table. When form submitted, bind the form with the User Entity. </p>
  
  <pre><code>    $user  = new \ZfcUser\Entity\User();
      $form  = $this-&gt;getRegisterForm();
      $form-&gt;setHydrator($this-&gt;getFormHydrator());
      $form-&gt;bind($user);
  
      $user = $form-&gt;getData();
  </code></pre>
  
  <p>Inserting is perform in two steps.</p>
  
  <pre><code>    $this-&gt;getUserMapper()-&gt;insert($user);
      $user_role_linker-&gt;setId($user-&gt;getId());
      $user_role_linker-&gt;setRoleId($data['user_role']);
      $this-&gt;getUserRoleLinkMapper()-&gt;insert($user_role_linker);
  </code></pre>
  
  <p>First insert the user and return the user_id,  then set user_id and user_role in <strong>UserRole</strong> entity and insert to table.</p>
  
  <p>But now the problem is to fetch two table to one object or one array. I may need to perform a table join to get the solution. But there is no way I could find to join tables with using ClassMethod hydrator. </p>
  
  <p>Please help me to accomplish this. </p>
  
  <p>UserHydrator class</p>
  
  <pre><code> namespace ZfcUser\Mapper;
  
   use Zend\Stdlib\Hydrator\ClassMethods;
   use ZfcUser\Entity\UserInterface as UserEntityInterface;
  
   class UserHydrator extends ClassMethods
   {
  /**
   * Extract values from an object
   *
   * @param  object $object
   * @return array
   * @throws Exception\InvalidArgumentException
   */
  public function extract($object)
  {
      if (!$object instanceof UserEntityInterface) {
          throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
      }
      /* @var $object UserInterface*/
      $data = parent::extract($object);
      $data = $this-&gt;mapField('id', 'user_id', $data);
      //$data = $this-&gt;mapField('id', 'username', $data);
      return $data;
  }
  
  /**
   * Hydrate $object with the provided $data.
   *
   * @param  array $data
   * @param  object $object
   * @return UserInterface
   * @throws Exception\InvalidArgumentException
   */
  public function hydrate(array $data, $object)
  {
      if (!$object instanceof UserEntityInterface) {
          throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
      }
      $data = $this-&gt;mapField('user_id', 'id', $data);
      return parent::hydrate($data, $object);
  }
  
  protected function mapField($keyFrom, $keyTo, array $array)
  {
      $array[$keyTo] = $array[$keyFrom];
      unset($array[$keyFrom]);
      return $array;
  }
  }
  </code></pre>