php - 如何使用选择框从数据库行 symfony2 中获取数据

标签 php mysql symfony doctrine twig

我目前正在使用 symfony2 进行一个小项目。我用 crud 命令做了一个简单的表。我有一个名为“voorraad”(=stock) 的实体,它与实体“product”和实体“Locatie”(=Location) 有关联。工作原理:我可以将产品和位置添加到我的库存中。

所以我的问题是,我无法弄清楚如何使用选择框按位置显示库存中的产品。我的想法是有一个选择框,其中包含我的位置实体中的位置,如果我选择一个选项,它将只显示我选择的产品。在我的代码下面:

Controller

<?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( View )

{% 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 %}

因此,在我的 Controller 中使用一个简单的代码,我设法获得了按位置订购的产品。

因此,对我来说,最后一步是使用选择框按位置显示产品,并从具有其他位置的列表中“删除”产品。下图是我到目前为止的结果,我想要这个列表上方的选择框。希望有人能帮助我..

So far..

最佳答案

您可以使用调用 AJAX 来调用另一个 Controller SF,该 Controller 使用新的 JSON 数据过滤您的结果和响应。

如果您的 AJAX 响应正确,您可以移动旧结果并使用 JS 添加新的 html 代码格式以查看结果选择框。

AJAX + controller SF = 在不重新加载的情况下更改结果网页

关于php - 如何使用选择框从数据库行 symfony2 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990811/

相关文章:

PHP:变量名作为类实例

与 LMS 兼容的 PHP 创作工具

php - 在 php 中将 html 转换为 pdf?

symfony - 如何在 Symfony 2.1 中使用 ChoiceList?

php - Guzzle with Xdebug 找不到他的函数

mysql - 在 mysql 表中查找日期范围

java - jpa 查询似乎没有正确解析枚举值并且总是返回空列表

mysql - Synology Diskstation 上的 "The service is disabled now."

php - 在关联(数组)上找到 Doctrine\Common\Collections\ArrayCollection 类型的实体,但期待(其他)

symfony - FOS UserBundle -> 无法创建新用户