[英]how to use a selectbox to fetch data from database row symfony2
我目前在symfony2中处理一个小项目。 我用crud命令做了一个简单的表。 我有一个名为“ voorraad”(= stock)的实体,该实体与实体“ product”和实体“ Locatie”(= Location)有关联。 工作原理:我可以在库存中添加产品和位置。
所以我的问题是,我无法弄清楚如何使用选择框按位置显示库存中的产品。 这个想法是要有一个带有我位置实体中位置的选择框,如果我选择一个选项,它将仅显示我选择的产品。 在我的代码下面:
控制者
<?php
namespace ToolsForEver\VoorraadBundle\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 ToolsForEver\VoorraadBundle\Entity\Voorraad;
use ToolsForEver\VoorraadBundle\Form\VoorraadType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
/**
* Voorraad controller.
*
* @Route("/voorraad")
*/
class VoorraadController extends Controller
{
/**
* Lists all Voorraad entities.
*
* @Route("/", name="voorraad")
* @Method("GET")
* @Template()
* @Security("has_role('ROLE_USER')")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ToolsForEverVoorraadBundle:Voorraad')->findBy(array(), array('locatie'=>'asc'));
return array(
'entities' => $entities,
);
}
/**
* Creates a new Voorraad entity.
*
* @Route("/", name="voorraad_create")
* @Method("POST")
* @Template("ToolsForEverVoorraadBundle:Voorraad:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Voorraad();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('voorraad_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Voorraad entity.
*
* @param Voorraad $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Voorraad $entity)
{
$form = $this->createForm(new VoorraadType(), $entity, array(
'action' => $this->generateUrl('voorraad_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Voorraad entity.
*
* @Route("/new", name="voorraad_new")
* @Method("GET")
* @Template()
* @Security("has_role('ROLE_USER')")
*/
public function newAction()
{
$entity = new Voorraad();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a Voorraad entity.
*
* @Route("/{id}", name="voorraad_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ToolsForEverVoorraadBundle:Voorraad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Voorraad entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Voorraad entity.
*
* @Route("/{id}/edit", name="voorraad_edit")
* @Method("GET")
* @Template()
* @Security("has_role('ROLE_USER')")
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ToolsForEverVoorraadBundle:Voorraad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Voorraad 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 Voorraad entity.
*
* @param Voorraad $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Voorraad $entity)
{
$form = $this->createForm(new VoorraadType(), $entity, array(
'action' => $this->generateUrl('voorraad_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Voorraad entity.
*
* @Route("/{id}", name="voorraad_update")
* @Method("PUT")
* @Template("ToolsForEverVoorraadBundle:Voorraad:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ToolsForEverVoorraadBundle:Voorraad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Voorraad entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('voorraad_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Voorraad entity.
*
* @Route("/{id}", name="voorraad_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('ToolsForEverVoorraadBundle:Voorraad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Voorraad entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('voorraad'));
}
/**
* Creates a form to delete a Voorraad 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('voorraad_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Verwijder voorraad'))
->getForm()
;
}
}
VoorraadType.php(窗体)
<?php
namespace ToolsForEver\VoorraadBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class VoorraadType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('aantal')
->add('locatie', 'entity', array (
'empty_data' => null,
'label' => 'Kies locatie',
'class' => 'ToolsForEver\VoorraadBundle\Entity\Locatie',
'choice_label' => function ($locatie) {
return $locatie->getLocatienaam();
}
))
->add('product', 'entity', array(
'empty_data' => null,
'label' => 'Kies product',
'class' => 'ToolsForEver\VoorraadBundle\Entity\Product',
'choice_label' => function ($product) {
return $product->getNaam();
}
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ToolsForEver\VoorraadBundle\Entity\Voorraad'
));
}
/**
* @return string
*/
public function getName()
{
return 'toolsforever_voorraadbundle_voorraad';
}
}
index.html.twig(查看)
{% extends '::base.html.twig' %}
{% block body -%}
<h1 class="hoofdtitel">Voorraad lijst</h1>
<table class="records_list">
<thead>
<tr>
<!-- <th>Id</th> -->
<th>Product</th>
<th>Type</th>
<th>Fabriek</th>
<th>Aantal</th>
<th>Inkoopprijs</th>
<th>Verkoopprijs
<th>Locatie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<!-- <td><a href="{{ path('voorraad_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td> -->
<td>{{ entity.getProduct().getNaam() }}</td>
<td>{{ entity.getProduct().getType() }}</td>
<td>{{ entity.getProduct().getFabriek() }}</td>
<td>{{ entity.aantal }}</td>
<td>{{ entity.getProduct().getInkoopprijs() }}</td>
<td>{{ entity.getProduct().getVerkoopprijs() }}</td>
<td>{{ entity.getLocatie().getLocatienaam() }}</td>
<td>
<a href="{{ path('voorraad_edit', { 'id': entity.id }) }}">Voorraad aanpassen</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<a href="{{ path('voorraad_new') }}">
Nieuwe voorraad toevoegen
</a>
{% endblock %}
因此,通过控制器中的简单代码,我设法按位置订购了产品。
因此,对我来说,最后一步是使用选择框按位置显示产品,然后从列表中的其他位置“删除”产品。 下图是到目前为止的结果,我希望该列表上方的选择框。 希望有人可以帮助我。
您可以使用调用AJAX来调用anothers控制器SF,以使用新的JSON数据过滤结果和响应。
如果您的回答AJAX是正确的,则可以移动旧结果,并使用JS添加新的html代码格式以查看结果选择框。
AJAX +控制器SF =更改结果网页,无需重新加载
Symfony Cookbook提供了以下示例: http : //symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
基本上,您需要使用“实体”字段的query_builder
属性来根据位置限制您的产品。 然后,当用户更改“位置”的值时,创建一个JS脚本,该脚本将在异步附加请求上提交表单,在响应中获得“产品”的选择框,并将其替换在页面内。 您还需要在表单中使用EventListeners,以动态更新字段。
但是,我最终发现此解决方案非常“繁重”,因为您必须经过整个表单提交过程才能获得产品列表。 为了改善这一点,您可以创建一个控制器操作,该操作将根据位置返回一个产品列表,并在更改位置时调用此路由。
但是在两种情况下,AJAX和Form EventListeners都是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.