title: zf2 form: populate select field with data coming from database
body: <p>I'm learning zf2 and I'm facing a problem involving 2 (eventually more) modules working together. Note, I've carefully read <a href="https://stackoverflow.com/questions/12764080/zf2-how-to-use-the-hydrator-exchangearray-to-populate-a-nested-object">this post</a> (and the related one) which helped me a lot. I'm going to explain a bit the problem:</p>
  
  <ul>
  <li>Using the first module (FrOption) an administrator can manage website form options. All options are stored in a db table like this:</li>
  </ul>
  
  <p>id|field_name|field_value<br />
  1|country|germany|<br />
  2|country|france|<br />
  3|gender|Male|<br />
  4|gender|Female|<br />
  5|tipo|Car|<br />
  6|tipo|Fly|<br />
  ...</p>
  
  <ul>
  <li>In my module (FrItem) I've built a form which needs some "field_name" fields.
  My "item" table is the following:</li>
  </ul>
  
  <p>id|name|id_tipo|<br />
  1|Fiat|5|<br />
  2|Lufthansa|6|<br />
  3|Ford|5|<br />
  4|Air France 6|<br />
  ...</p>
  
  <p>(id_tipo is an option FK)</p>
  
  <p>Also consider:</p>
  
  <ul>
  <li>My entity has the "tipo" property, setter + getter</li>
  <li>I've built an ItemHydrator to "map" id_tipo db field to "tipo" entity property</li>
  <li><p>As a test, I've added this field in my form class and everything works fine both in view and edit mode:</p>
  
  <pre><code>$this-&gt;add(
  'type' =&gt; 'Zend\Form\Element\Select',
  'name' =&gt; 'id_tipo',
  'options' =&gt; array (
      'label' =&gt; 'Tipo', 
      'empty_option' =&gt; 'Select',
      'value_options' =&gt; array ('5' =&gt; 'Car', '6' =&gt; 'Fly' ) )
  </code></pre>
  
  <p>);</p></li>
  </ul>
  
  <p>Now i want to "link" the two modules: value_options should be a dynamic array coming from FrOption so I'm looking for the the best way to accomplish this requirement. </p>
  
  <p>I thought one solution could be something like this:</p>
  
  <ol>
  <li>Add to my FrOption/src/FrOption/Service/FrOption.php Service class the getOptionByName($fieldName) method</li>
  <li>In FrItem/Module.php retrieve the Service, then the data using getOptionByName and finally inject all into the Form.</li>
  </ol>
  
  <p>Could this be a wise and working solution? What do you thing about it also in terms of performance (option table could grow)? 
  If any, what kind of solution have you used to solve a similar problem?</p>
  
  <p>Thanks</p>