title: Symfony2 dropdown form from database
body: <p>So i want to display all the courses within my database in a drop down form. a user who is logged in then select one of the drop downs and it would store the users ID and the course ID into the database. How would i go about doing this?</p>
  
  <p>Here's my course entity course.php</p>
  
  <pre><code>&lt;?php
  // src/Simple/SimpleBundle/Entity/Course.php
  namespace Simple\ProfileBundle\Entity;
  
  use Symfony\Component\Security\Core\User\UserInterface;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
  * @ORM\Entity
  * @ORM\Table(name="course")
  */
  class Course
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\Column(name="course", type="string", length=255)
   */
  protected $course;
  
  
  public function __construct()
  {
      $this-&gt;users = new ArrayCollection();
  }
  
  
     public function setCourse($course)
  {
      $this-&gt;course = $course;
  }
  
  public function getCourse()
  {
      return $this-&gt;course;
  }
  
   public function setId($id)
  {
      $this-&gt;id = $id;
  }
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  }
  </code></pre>
  
  <p>ChooseCourseType.php my form</p>
  
  <pre><code>&lt;?php
  // src/Simple/ProfileBundle/Controller/ChooseCourseType.php
  namespace Simple\ProfileBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  
  class ChooseCourseType extends AbstractType
  {
  private $course;
  
  public function buildForm(FormBuilderInterface $builder, array $options) {
  
  
  $builder-&gt;add('course', 'choice', array(
      'choices'   =&gt; $this-&gt;course,
  ));
  
  $builder-&gt;add('Choose Course', 'submit');
  
  }
  public function getName()
  {
      return 'name';
  }     
  public function getCourse()
  {
      return 'course';
  }  
  
  
  
  }
  </code></pre>
  
  <p>Coursecontroller.php</p>
  
  <pre><code>function chooseAction(Request $request)
  {
  $em = $this-&gt;getDoctrine()-&gt;getManager();
  
  $form = $this-&gt;createForm(new ChooseCourseType(), $CourseType);
  
  $form-&gt;handleRequest($request);
  
  if ($form-&gt;isValid()) {
      $course = $form-&gt;getData();
  
      $em-&gt;persist($course-&gt;getCourse());
      $em-&gt;flush();
  
  
  }
  
  return $this-&gt;render(
      'SimpleProfileBundle:Course:choosecourse.html.twig',
      array('form' =&gt; $form-&gt;createView())
  );
  }
  
  }
  </code></pre>
  
  <p>User.php
      
  
  <pre><code>namespace Simple\ProfileBundle\Entity;
  use Symfony\Component\Serializer\Serializer;
  use Symfony\Component\Serializer\Encoder\XmlEncoder;
  use Symfony\Component\Serializer\Encoder\JsonEncoder;
  use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
  * @ORM\Entity
  * @ORM\Table(name="users")
  */
  class User implements UserInterface
  {
  
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\Column(name="user", type="string", length=255)
   */
  protected $username;
  
  /**
   * @ORM\Column(name="password", type="string", length=255)
   */
  protected $password;
  
  /**
   * @ORM\Column(name="salt", type="string", length=255)
   */
  protected $salt;
  
  /**
   * @ORM\ManyToMany(targetEntity="Role")
   * @ORM\JoinTable(name="user_role",
   *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
   * )
   */
  protected $roles;
  
        /**
   * @ORM\ManyToMany(targetEntity="Course")
   * @ORM\JoinTable(name="user_course",
   *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *     inverseJoinColumns={@ORM\JoinColumn(name="course_id",   
   referencedColumnName="id")}
   * )
   */
  protected $courses;
  /**
   * @inheritDoc
   */
  public function getUsername()
  {
      return $this-&gt;username;
  }
  
  /**
   * @inheritDoc
   */
  public function getSalt()
  {
      return '';
  }
  
  /**
   * @inheritDoc
   */
  public function getPassword()
  {
      return $this-&gt;password;
  }
  
  /**
   * @inheritDoc
   */
  public function getRoles()
  {
  return $this-&gt;roles-&gt;toArray();
  }
  
    /**
   * @inheritDoc
   */
  public function eraseCredentials()
  {
  }
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;roles = new \Doctrine\Common\Collections\ArrayCollection();
      $this-&gt;salt = sha1(uniqid(null, true));
  }
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set user
   *
   * @param string $user
   * @return User
   */
  public function setUser($user)
  {
      $this-&gt;user = $user;
  
      return $this;
  }
  
  /**
   * Get user
   *
   * @return string 
   */
  public function getUser()
  {
      return $this-&gt;user;
  }
  
  /**
   * Set password
   *
   * @param string $password
   * @return User
   */
  public function setPassword($password)
  {
      $this-&gt;password = $password;
  
      return $this;
  }
  
  /**
   * Set salt
   *
   * @param string $salt
   * @return User
   */
  public function setSalt($salt)
  {
      $this-&gt;salt = $salt;
  
      return $this;
  }
  
  /**
   * Add roles
   *
   * @param \Simple\ProfileBundle\Entity\Role $roles
   * @return User
   */
  public function addRole(\Simple\ProfileBundle\Entity\Role $roles)
  {
      $this-&gt;roles[] = $roles;
  
      return $this;
  }
  
  /**
   * Remove roles
   *
   * @param \Simple\ProfileBundle\Entity\Role $roles
   */
  public function removeRole(\Simple\ProfileBundle\Entity\Role $roles)
  {
      $this-&gt;roles-&gt;removeElement($roles);
  }
  
      /**
   * Add roles
   *
   * @param \Simple\ProfileBundle\Entity\Course $courses
   * @return User
   */
  public function addCourse(\Simple\ProfileBundle\Entity\Course $courses)
  {
      $this-&gt;course[] = $courses;
  
      return $this;
  }
  
  
  
  }
  </code></pre>
  
  <p>What else am i missing i am new to symfony2 and just need some direction.</p>
  
  <p>Cheers</p>