php - Displaying clicked items in session array -
i'm learning mvc , symfony2 (version 2.7) creating super simple shopping cart test (i know symfony has doctrine want learn basics, go easy on me ;) ).
all want able each item click, appears next other items clicked. isn't mvc or symfony2 problem as php, twig coding problem i'm flubbing on.
i have form users can click buy, on users redirected form displays bought. below display other items buy button option again. once click button item, new item should appear next previous one.
instead old item goes away , new item appears.
how can make old ones stay along new ones?
below controller class renders forms. take @ buyaction stores items in cart, 1 @ time...how can fix that?
//////////////////
<?php namespace appbundle\controller; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\session\session; 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 item entities. * * @route("/", name="item") * @method("get") * @template() */ public function indexaction() { $em = $this->getdoctrine()->getmanager(); $entities = $em->getrepository('appbundle:item')->findall(); return array( 'entities' => $entities, ); } /** * creates 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->createcreateform($entity); $form->handlerequest($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($entity); $em->flush(); return $this->redirect($this->generateurl('item_show', array('id' => $entity->getid()))); } return array( 'entity' => $entity, 'form' => $form->createview(), ); } /** * creates form create item entity. * * @param item $entity entity * * @return \symfony\component\form\form form */ private function createcreateform(item $entity) { $form = $this->createform(new itemtype(), $entity, array( 'action' => $this->generateurl('item_create'), 'method' => 'post', )); $form->add('submit', 'submit', array('label' => 'create')); return $form; } /** * displays form create new item entity. * * @route("/new", name="item_new") * @method("get") * @template() */ public function newaction() { $entity = new item(); $form = $this->createcreateform($entity); return array( 'entity' => $entity, 'form' => $form->createview(), ); } /** * displays bought items * * @route("/buy/{id}", name="item_buy") * @method("get") * @template() */ public function buyaction(request $request, $id) { $em = $this->getdoctrine()->getmanager(); $entity = $em->getrepository('appbundle:item')->find($id); $entities = $em->getrepository('appbundle:item')->findall(); $cart = []; $session = $request->getsession(); $session->set('cart', $cart); if (!$entity) { throw $this->createnotfoundexception('no item '); } else { $cart[$entity->getid()] = $entity->getname(); } $session->set('cart', $cart); return array( 'cart' => $cart, 'entity' => $entity, 'entities' => $entities, ); } /** * finds , displays item entity. * * @route("/show/{id}", name="item_show") * @method("get") * @template() */ public function showaction($id) { $em = $this->getdoctrine()->getmanager(); $entity = $em->getrepository('appbundle:item')->find($id); if (!$entity) { throw $this->createnotfoundexception('unable find item entity.'); } $deleteform = $this->createdeleteform($id); return array( 'entity' => $entity, 'delete_form' => $deleteform->createview(), ); } /** * displays form edit existing item entity. * * @route("/{id}/edit", name="item_edit") * @method("get") * @template() */ public function editaction($id) { $em = $this->getdoctrine()->getmanager(); $entity = $em->getrepository('appbundle:item')->find($id); if (!$entity) { throw $this->createnotfoundexception('unable find item entity.'); } $editform = $this->createeditform($entity); $deleteform = $this->createdeleteform($id); return array( 'entity' => $entity, 'edit_form' => $editform->createview(), 'delete_form' => $deleteform->createview(), ); } /** * creates form edit item entity. * * @param item $entity entity * * @return \symfony\component\form\form form */ private function createeditform(item $entity) { $form = $this->createform(new itemtype(), $entity, array( 'action' => $this->generateurl('item_update', array('id' => $entity->getid())), 'method' => 'put', )); $form->add('submit', 'submit', array('label' => 'update')); return $form; } /** * edits existing item entity. * * @route("/{id}", name="item_update") * @method("put") * @template("appbundle:item:edit.html.twig") */ public function updateaction(request $request, $id) { $em = $this->getdoctrine()->getmanager(); $entity = $em->getrepository('appbundle:item')->find($id); if (!$entity) { throw $this->createnotfoundexception('unable find item entity.'); } $deleteform = $this->createdeleteform($id); $editform = $this->createeditform($entity); $editform->handlerequest($request); if ($editform->isvalid()) { $em->flush(); return $this->redirect($this->generateurl('item_edit', array('id' => $id))); } return array( 'entity' => $entity, 'edit_form' => $editform->createview(), 'delete_form' => $deleteform->createview(), ); } /** * deletes item entity. * * @route("/{id}", name="item_delete") * @method("delete") */ public function deleteaction(request $request, $id) { $form = $this->createdeleteform($id); $form->handlerequest($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $entity = $em->getrepository('appbundle:item')->find($id); if (!$entity) { throw $this->createnotfoundexception('unable find item entity.'); } $em->remove($entity); $em->flush(); } return $this->redirect($this->generateurl('item')); } /** * creates form delete item entity id. * * @param mixed $id entity id * * @return \symfony\component\form\form form */ private function createdeleteform($id) { return $this->createformbuilder() ->setaction($this->generateurl('item_delete', array('id' => $id))) ->setmethod('delete') ->add('submit', 'submit', array('label' => 'delete')) ->getform() ; } }
///////////////// below first form, nothing "bought" yet. //////////////////
{% extends '::base.html.twig' %} {% block body -%} <h1>item list</h1> <table class="records_list"> <thead> <tr> <th>id</th> <th>name</th> <th>actions</th> </tr> </thead> <tbody> {% entity in entities %} <tr> <td><a href="{{ path('item_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td> <td>{{ entity.name }}</td> <td> <ul> <li> <a href="{{ path('item_buy', { 'id': entity.id }) }}">buy</a> </li> <li> <a href="{{ path('item_show', { 'id': entity.id }) }}">show</a> </li> <li> <a href="{{ path('item_edit', { 'id': entity.id }) }}">edit</a> </li> </ul> </td> </tr> {% endfor %} </tbody> </table> <ul> <li> <a href="{{ path('item_new') }}">create new entry</a> </li> {#<li> <a href="{{ path('item') }}">buy item</a> </li>#} </ul> {% endblock %}
///////////////////// below form users shown after "bought" item (where "bought" item displayed top) , can choose "buy" else (where new item should go next previous "bought" item).
////////////////////
{% extends '::base.html.twig' %} {% block body -%} <h1>items bought...</h1> <table class="record_properties"> <h3>you bought...</h3> {# {% if entity defined %} #} {% entities in cart %} <tr> <td>{{ entity.name }}</td> </tr> {% endfor %} {# {% endif %} #} <tbody> {% entity in entities %} <tr> <td><a href="{{ path('item_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td> <td>{{ entity.name }}</td> <td> <ul> <li> <a href="{{ path('item_buy', { 'id': entity.id }) }}">buy</a> </li> </ul> </td> </tr> {% endfor %} </tbody> </table> <ul class="record_actions"> <li> <a href="{{ path('item') }}"> list </a> </li> {% endblock %}
//////////////////////
i've been starring @ day yesterday , can't find issue. appreciated, thanks.
also ideas how make better/things me learn mvc basics/symfony2 stuff cool if got too. i've been using lynda dot com, google, site, symfony book/cookbook. thanks!
the problem have in last twig file.
{% entities in cart %}
you're placing in entities variable each iteration of cart, you're losing entities loaded in controller.
change this...
{% entities in cart %} <tr> <td>{{ entity.name }}</td> </tr> {% endfor %}
to this...
{% cartitem in cart %} <tr> <td>{{ cartitem.name }}</td> </tr> {% endfor %}
at least part should solved :)
Comments
Post a Comment