title: How to fetch items from a database using symfony2
body: <p>I'm new to MVC and am trying to learn it by doing an assignment.</p>
  
  <p>Here's what I'm trying to do:
  I want users to interact with a form that has the options to 'add items to a "cart"' (yea I know there is a doctrine for this in Symfony2 but I don't want to use it, I'm just trying to really understand the MVC framework here and build something simple). Once the user adds all the items to the "cart" they can click a button that says buy. Once they click that button, a new page is shown displaying all the items they added in their "cart". </p>
  
  <p>Here's what is working: 
  A form is displayed with 'items' entered in from another form. CRUD functionality is working, the users can edit or delete the item from this displayed form. </p>
  
  <p>What's not working is my adding to "cart" aspect. How do I get the items out of the db and into an array once the user clicks add to cart?</p>
  
  <p>What I think I need to do:
  I know I need a controller method to take the items that have been selected out of the db and store them in an array, or something like that. But I'm lost on how to do this. When searching for answers, they are all more complex than what I need and the symfony book/cookbook doesn't really help me either. Also I'm using version 2.7 of symfony2. </p>
  
  <p>Below are my controller classes, as well as my Entity files (I have corresponding repository files for them as well). If anything else is needed, let me know. </p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Item
   *
   * @ORM\Table(name="items")
   * @ORM\Entity(repositoryClass="AppBundle\Entity\ItemRepository")
   */
  class Item
  {
  
      /**
       * @var ArrayCollection
       *
       * @ORM\OneToMany(targetEntity="Description", mappedBy="item")
       */
      private $descriptions;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      public function __construct() {
          $this-&gt;descriptions = new ArrayCollection();
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Item
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Add descriptions
       *
       * @param \AppBundle\Entity\Description $descriptions
       * @return Item
       */
      public function addDescription(\AppBundle\Entity\Description $descriptions)
      {
          $this-&gt;descriptions[] = $descriptions;
  
          return $this;
      }
  
      /**
       * Remove descriptions
       *
       * @param \AppBundle\Entity\Description $descriptions
       */
      public function removeDescription(\AppBundle\Entity\Description $descriptions)
      {
          $this-&gt;descriptions-&gt;removeElement($descriptions);
      }
  
      /**
       * Get descriptions
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getDescriptions()
      {
          return $this-&gt;descriptions;
      }
  
      /**
       * Render a Item as a string
       *
       * @return string
       */
      public function __toString() {
          return $this-&gt;getName();
      }
  }
  </code></pre>
  
  <p>/////////////////////
          
  
  <pre><code>namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Doctrine\Common\Collection\ArrayCollection;
  
  /**
   * Description
   *
   * @ORM\Table(name="descriptions")
   * @ORM\Entity(repositoryClass="AppBundle\Entity\DescriptionRepository")
   */
  class Description
  {
  
      /**
       * @var Item
       *
       * @ORM\ManyToOne(targetEntity="Item", inversedBy="descriptions")
       * @ORM\JoinColumn(name="item_id", referencedColumnName="id", onDelete="cascade")
       */
      private $item;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="price", type="integer")
       * @Assert\Range(
       *  min = 1,
       *  minMessage = "Price needs to be 1 or higher."
       * )
       */
      private $price;
  
      /**
       * @var string
       *
       * @ORM\Column(name="info", type="string", length=255)
       */
      private $info;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set price
       *
       * @param integer $price
       * @return Description
       */
      public function setPrice($price)
      {
          $this-&gt;price = $price;
  
          return $this;
      }
  
      /**
       * Get price
       *
       * @return integer 
       */
      public function getPrice()
      {
          return $this-&gt;price;
      }
  
      /**
       * Set info
       *
       * @param string $info
       * @return Description
       */
      public function setInfo($info)
      {
          $this-&gt;info = $info;
  
          return $this;
      }
  
      /**
       * Get info
       *
       * @return string 
       */
      public function getInfo()
      {
          return $this-&gt;info;
      }
  
      /**
       * Set item
       *
       * @param \AppBundle\Entity\Item $item
       * @return Description
       */
      public function setItem(\AppBundle\Entity\Item $item = null)
      {
          $this-&gt;item = $item;
  
          return $this;
      }
  
      /**
       * Get item
       *
       * @return \AppBundle\Entity\Item 
       */
      public function getItem()
      {
          return $this-&gt;item;
      }
  }
  </code></pre>
  
  <p>///////////////////////
  Look below at addAction function....
  ////////////////</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use AppBundle\Entity\Item;
  use AppBundle\Form\ItemType;
  
  /**
   * Item controller.
   *
   * @Route("/item")
   */
  class ItemController extends Controller
  {
  
      /**
       * Lists all Item entities.
       *
       * @Route("/", name="item")
       * @Method("GET")
       * @Template()
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('AppBundle:Item')-&gt;findAll();
  
          return array(
              'entities' =&gt; $entities,
          );
      }
  
      /**
       * Creates a new Item entity.
       *
       * @Route("/", name="item_create")
       * @Method("POST")
       * @Template("AppBundle:Item:new.html.twig")
       */
      public function createAction(Request $request)
      {
          $entity = new Item();
          $form = $this-&gt;createCreateForm($entity);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('item_show', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Creates a form to create a Item entity.
       *
       * @param Item $entity The entity
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createCreateForm(Item $entity)
      {
          $form = $this-&gt;createForm(new ItemType(), $entity, array(
              'action' =&gt; $this-&gt;generateUrl('item_create'),
              'method' =&gt; 'POST',
          ));
  
          $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  
          return $form;
      }
  
      /**
       * Displays a form to create a new Item entity.
       *
       * @Route("/new", name="item_new")
       * @Method("GET")
       * @Template()
       */
      public function newAction()
      {
          $entity = new Item();
          $form   = $this-&gt;createCreateForm($entity);
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Adds Corresponding Item to Cart
       *
       * @Route("addAction")
       * @Method("GET")
       * @Template()
       */
      public function addAction($id) {
          //this method should just take item out of db and list and add the item to cart
          //buy button should be at very bottom of item list form and then use items from add button and
          //form should be rendered saying all items (list them out) have been bought.
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Item')-&gt;find($id);
  
          if (!$item) {
              throw $this-&gt;createNotFoundException('No Item ');
          }
  
          $query = $em-&gt;createQuery(
              'SELECT items as $id
              FROM AppBundle:Item'
          );
      }
  
      /**
      * Creates a form for buying items
      *
      * @param Item $entity The entity
      *
      * @return \Symfony\Component\Form\Form The form
      */
      public function createBuyForm(Item $entity) {
          $form = $this-&gt;createForm(new ItemType(), $entity, array(
              'action' =&gt; $this-&gt;generateUrl('item_buy', array('id' =&gt; $entity-&gt;getId())),
              'method' =&gt; 'PUT',
  
          ));
  
          $form-&gt;add('submit', 'submit', array('label' =&gt; 'Buy'));
  
          return $form;
      }
  
  
      /**
       * Finds and displays an Item entity.
       *
       * @Route("/{id}", name="item_show")
       * @Method("GET")
       * @Template()
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Item')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Item entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to edit an existing Item entity.
       *
       * @Route("/{id}/edit", name="item_edit")
       * @Method("GET")
       * @Template()
       */
      public function editAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Item')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Item entity.');
          }
  
          $editForm = $this-&gt;createEditForm($entity);
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
      * Creates a form to edit a Item entity.
      *
      * @param Item $entity The entity
      *
      * @return \Symfony\Component\Form\Form The form
      */
      private function createEditForm(Item $entity)
      {
          $form = $this-&gt;createForm(new ItemType(), $entity, array(
              'action' =&gt; $this-&gt;generateUrl('item_update', array('id' =&gt; $entity-&gt;getId())),
              'method' =&gt; 'PUT',
          ));
  
          $form-&gt;add('submit', 'submit', array('label' =&gt; 'Update'));
  
          return $form;
      }
  
      /**
       * Edits an existing Item entity.
       *
       * @Route("/{id}", name="item_update")
       * @Method("PUT")
       * @Template("AppBundle:Item:edit.html.twig")
       */
      public function updateAction(Request $request, $id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Item')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Item entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
          $editForm = $this-&gt;createEditForm($entity);
          $editForm-&gt;handleRequest($request);
  
          if ($editForm-&gt;isValid()) {
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('item_edit', array('id' =&gt; $id)));
          }
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Deletes a Item entity.
       *
       * @Route("/{id}", name="item_delete")
       * @Method("DELETE")
       */
      public function deleteAction(Request $request, $id)
      {
          $form = $this-&gt;createDeleteForm($id);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $entity = $em-&gt;getRepository('AppBundle:Item')-&gt;find($id);
  
              if (!$entity) {
                  throw $this-&gt;createNotFoundException('Unable to find Item entity.');
              }
  
              $em-&gt;remove($entity);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirect($this-&gt;generateUrl('item'));
      }
  
      /**
       * Creates a form to delete a Item entity by id.
       *
       * @param mixed $id The entity id
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createDeleteForm($id)
      {
          return $this-&gt;createFormBuilder()
              -&gt;setAction($this-&gt;generateUrl('item_delete', array('id' =&gt; $id)))
              -&gt;setMethod('DELETE')
              -&gt;add('submit', 'submit', array('label' =&gt; 'Delete'))
              -&gt;getForm()
          ;
      }
  }
  </code></pre>
  
  <p>//////////////////////////</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use AppBundle\Entity\Description;
  use AppBundle\Form\DescriptionType;
  
  /**
   * Description controller.
   *
   * @Route("/description")
   */
  class DescriptionController extends Controller
  {
  
      /**
       * Lists all Description entities.
       *
       * @Route("/", name="description")
       * @Method("GET")
       * @Template()
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('AppBundle:Description')-&gt;findAll();
  
          return array(
              'entities' =&gt; $entities,
          );
      }
      /**
       * Creates a new Description entity.
       *
       * @Route("/", name="description_create")
       * @Method("POST")
       * @Template("AppBundle:Description:new.html.twig")
       */
      public function createAction(Request $request)
      {
          $entity = new Description();
          $form = $this-&gt;createCreateForm($entity);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('description_show', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Creates a form to create a Description entity.
       *
       * @param Description $entity The entity
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createCreateForm(Description $entity)
      {
          $form = $this-&gt;createForm(new DescriptionType(), $entity, array(
              'action' =&gt; $this-&gt;generateUrl('description_create'),
              'method' =&gt; 'POST',
          ));
  
          $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  
          return $form;
      }
  
      /**
       * Displays a form to create a new Description entity.
       *
       * @Route("/new", name="description_new")
       * @Method("GET")
       * @Template()
       */
      public function newAction()
      {
          $entity = new Description();
          $form   = $this-&gt;createCreateForm($entity);
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Finds and displays a Description entity.
       *
       * @Route("/{id}", name="description_show")
       * @Method("GET")
       * @Template()
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Description')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Description entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to edit an existing Description entity.
       *
       * @Route("/{id}/edit", name="description_edit")
       * @Method("GET")
       * @Template()
       */
      public function editAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Description')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Description entity.');
          }
  
          $editForm = $this-&gt;createEditForm($entity);
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
      * Creates a form to edit a Description entity.
      *
      * @param Description $entity The entity
      *
      * @return \Symfony\Component\Form\Form The form
      */
      private function createEditForm(Description $entity)
      {
          $form = $this-&gt;createForm(new DescriptionType(), $entity, array(
              'action' =&gt; $this-&gt;generateUrl('description_update', array('id' =&gt; $entity-&gt;getId())),
              'method' =&gt; 'PUT',
          ));
  
          $form-&gt;add('submit', 'submit', array('label' =&gt; 'Update'));
  
          return $form;
      }
      /**
       * Edits an existing Description entity.
       *
       * @Route("/{id}", name="description_update")
       * @Method("PUT")
       * @Template("AppBundle:Description:edit.html.twig")
       */
      public function updateAction(Request $request, $id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('AppBundle:Description')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Description entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
          $editForm = $this-&gt;createEditForm($entity);
          $editForm-&gt;handleRequest($request);
  
          if ($editForm-&gt;isValid()) {
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('description_edit', array('id' =&gt; $id)));
          }
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
      /**
       * Deletes a Description entity.
       *
       * @Route("/{id}", name="description_delete")
       * @Method("DELETE")
       */
      public function deleteAction(Request $request, $id)
      {
          $form = $this-&gt;createDeleteForm($id);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $entity = $em-&gt;getRepository('AppBundle:Description')-&gt;find($id);
  
              if (!$entity) {
                  throw $this-&gt;createNotFoundException('Unable to find Description entity.');
              }
  
              $em-&gt;remove($entity);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirect($this-&gt;generateUrl('description'));
      }
  
      /**
       * Creates a form to delete a Description entity by id.
       *
       * @param mixed $id The entity id
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createDeleteForm($id)
      {
          return $this-&gt;createFormBuilder()
              -&gt;setAction($this-&gt;generateUrl('description_delete', array('id' =&gt; $id)))
              -&gt;setMethod('DELETE')
              -&gt;add('submit', 'submit', array('label' =&gt; 'Delete'))
              -&gt;getForm()
          ;
      }
  }
  </code></pre>