title: How to fill a Symfony choice widget dynamically with the result of a DB query
body: <p>In one of my forms I would like to have a dropdown (sfWidgetFormChoice) where the options are generated dynamically by executing a query on the database.</p>
  <p>To be a little bit more precise, I'm going to list all versions which I have in the table. The query looks something like this:</p>
  <pre><code>select distinct version from mytable order by version desc
  </code></pre>
  <p>What I have so far but doesn't work:</p>
  <pre><code>class myForm extends sfForm
  
  $query = &quot;select distinct version from mytable order by version desc&quot;;
  
  $versions = Doctrine_Manager::getInstance()-&gt;getCurrentConnection()-&gt;fetchAssoc($query);
  
  public function configure()
  
  $this-&gt;setWidgets(array('version' =&gt; new sfWidgetFormChoice(array('choices' =&gt; self::$versions))));
  </code></pre>
  <p><strong>Edit:</strong></p>
  <p>Thansk guys for your answers! Much appreciated!</p>
  <p>Anyhow your solutions base on having a model of the table. I'd rather have a PDO directly as it's faster.</p>
  <p>In the <a href="http://www.symfony-project.org/gentle-introduction/1_4/en/08-Inside-the-Model-Layer-Doctrine" rel="nofollow noreferrer">Symfony Documentation</a> I found what I was looking for under &quot;Using Raw SQL Queries&quot;.</p>
  <p>So I extended my Form to end up with this:</p>
  <pre><code>class myForm extends sfForm
  {
    public function getVersions()
    {
      $connection = Doctrine_Manager::connection();
      $query      = &quot;select distinct version from mytable order by version desc&quot;;
      $statement  = $connection-&gt;prepare($query);
      $statement-&gt;execute();
      $resultset = $statement-&gt;fetchAll(PDO::FETCH_COLUMN, 0);
  
      return $resultset;
    }
  
    public function configure()
    {
      $this-&gt;setWidgets(array('version' =&gt; new sfWidgetFormChoice(array('choices' =&gt; self::getVersions()))));
    }
  }
  </code></pre>
  <p>As a result of this my dropdown gets properly filled with what is in my table, yay! BUT I get also warnings:</p>
  <blockquote>
  <p>Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 196</p>
  <p>Warning: join() [function.join]: Invalid arguments passed in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 141</p>
  <p>Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 196</p>
  <p>Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/debug/sfDoctrineConnectionProfiler.class.php on line 117</p>
  </blockquote>
  <p>Any idea what I'm doing wrong here??? Oddly enough the dropdown looks fine.</p>