title: Yii: How to populate a Select input with another model data?
body: <p>I'm playing around with a small app in order to learn to use Yii. </p>
  
  <p>I've created a small webapp  with  2 models / tables: Projects and tasks. (1-to-many relationship, properly configured in the model classes).</p>
  
  <p>I'm now trying to customize the Task/create view, replacing the text input field with a select box proposing the list of available projects.</p>
  
  <p>I opened the form view and tried this:</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;?php echo $form-&gt;labelEx($model,'project_id'); ?&gt;
      &lt;?php echo $form-&gt;textField($model,'project_id'); ?&gt;
      &lt;?php 
  // my hack starts here
      $projects = Project::model()-&gt;findAll();
      $list = CHtml::listData($projects, 'id', 'name');
      echo $form-&gt;listBox($model,'project_id','', $list); ?&gt;
  
  // my hack ends here
      &lt;?php echo $form-&gt;error($model,'project_id'); ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But it keeps throwing warnings or error (such as <code>Invalid argument supplied for foreach()</code>, and definitely does not work. I'm failing to understand what i'm doing wrong. Can you help ?</p>