dongqiu7365 2016-01-17 21:13
浏览 42
已采纳

如何在Twig / Symfony2中按对象/表头排序表

I am working on a project with a table generated from crud command. See my code below from my "Voorraad" entity

index.html.twig

{% extends '::base.html.twig' %}

{% block body -%}
<h1>Voorraad lijst</h1>

<table class="records_list">
    <thead>
        <tr>
            <!-- <th>Id</th> -->
            <th>Product</th>
            <th>Type</th>
            <th>Fabriek</th>
            <th>Inkoopprijs</th>
            <th>Verkoopprijs
            <th>Aantal</th>
            <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.getProduct().getInkoopprijs() }}</td>
            <td>{{ entity.getProduct().getVerkoopprijs() }}</td>
            <td>{{ entity.aantal }}</td>
            <td>{{ entity.getLocatie().getLocatienaam() }}</td>
            <td>

                    <a href="{{ path('voorraad_edit', { 'id': entity.id }) }}">Voorraad aanpassen</a>

            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

    <ul>
    <li>
        <a href="{{ path('voorraad_new') }}">
            Nieuwe voorraad toevoegen   
        </a>
    </li>
</ul>
{% endblock %}

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;

/**
 * Voorraad controller.
 *
 * @Route("/voorraad")
 */
class VoorraadController extends Controller
{

/**
 * Lists all Voorraad entities.
 *
 * @Route("/", name="voorraad")
 * @Method("GET")
 * @Template()
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('ToolsForEverVoorraadBundle:Voorraad')->findAll();

    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()
 */
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()
 */
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' => 'Delete'))
        ->getForm()
    ;
}
}

I would like to order the table by "Locatie" (=Location), the following tablehead.

<th>Locatie</th>

Locatie is a foreign key of the entity "Voorraad", and primary key of the entity "Locatie". I also have a entitiy called "Product". Unfortunately I do not have any idea how to get this done..

  • 写回答

2条回答 默认 最新

  • dongzhuang1923 2016-01-17 21:33
    关注

    In your controller that feeds your view you need to sort the entities;

     $this->get('doctrine')->getRepository('YourBundle\Entity\YourEntity')->findBy(array(), array('Locatie', 'ASC'));
    

    See here;

    http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html

    The EntityRepository#findBy() method additionally accepts orderings, limit and offset as second to fourth parameters:

    I am answering this very bluntly as you need to provide more details on your entity and the 'Locatie' association (entity.getLocatie().getLocatienaam())

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀