我正在通过创建一个超级简单的购物车测试来学习MVC和Symfony2(版本2.7((我知道symfony对此有一个原则,但我只想学习基础知识,所以;)放轻松(。
我希望能够做的是,对于他们单击的每个项目,它出现在他们单击的其他项目旁边。这真的不是一个MVC或Symfony2的问题,而是一个php,twig编码问题,我正在研究这个问题。
我有一个用户可以单击购买的表单,用户被重定向到另一个显示他们购买内容的表单。该显示屏下方是再次带有购买按钮选项的其他项目。一旦他们单击某个项目的按钮,新项目应出现在前一个项目旁边。
相反,旧项目消失,新项目出现。
我怎样才能让旧的和新的保持在一起?
下面是呈现窗体的控制器类。看看买行动它将物品存储在购物车中,但一次只能存储一个......我该如何解决这个问题?
////
<?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 all 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 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->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 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->createForm(new ItemType(), $entity, array(
'action' => $this->generateUrl('item_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => '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->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 and displays an 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 to find Item entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->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->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Item')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Item entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->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->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 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->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Item')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to 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 an 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 to find Item entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('item'));
}
/**
* Creates a form to delete an Item entity by id.
*
* @param mixed $id The entity id
*
* @return 'Symfony'Component'Form'Form The 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()
;
}
}
///下面是第一种形式,其中还没有"购买"任何东西。//////////////////
{% 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>
{% for 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 a new entry</a>
</li>
{#<li>
<a href="{{ path('item') }}">Buy Item</a>
</li>#}
</ul>
{% endblock %}
///下面是用户在"购买"项目后显示的表单(其中"购买"项目显示在顶部(以及他们可以选择"购买"其他项目的位置(此新项目应放在上一个"购买"项目旁边(。
////
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Items Bought...</h1>
<table class="record_properties">
<h3>You Bought...</h3>
{# {% if entity is defined %} #}
{% for entities in cart %}
<tr>
<td>{{ entity.name }}</td>
</tr>
{% endfor %}
{# {% endif %} #}
<tbody>
{% for 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') }}">
Back to the list
</a>
</li>
{% endblock %}
///
我昨天盯着这个看了一整天,我找不到我的问题。因此,任何帮助都值得赞赏,谢谢。
也任何如何使它变得更好的想法/帮助我学习MVC基础知识/Symfony2的东西,如果你也有一些,那将是很酷的。我一直在使用Lynda dot com,google,这个网站,Symfony书/食谱。谢谢!
问题是你在最后一个 Twig 文件中有这个。
{% for entities in cart %}
您在购物车的每次迭代中都放置实体变量,因此您将丢失控制器中加载的所有实体。
改变这个...
{% for entities in cart %}
<tr>
<td>{{ entity.name }}</td>
</tr>
{% endfor %}
对此...
{% for cartItem in cart %}
<tr>
<td>{{ cartItem.name }}</td>
</tr>
{% endfor %}
至少这部分应该解决:)