2016-01-17 111 views
0

我正在使用從crud命令生成的表格處理項目。參見下面我的代碼來自我的 「Voorraad」 實體如何根據Twig/Symfony2中的對象/表頭對錶格進行排序

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

控制器

<?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() 
    ; 
} 
} 

我想通過 「Locatie」(=位置)訂購表,以下表格。

<th>Locatie</th> 

Locatie是實體「Voorraad」的外鍵和實體「Locatie」的主鍵。我也有一個名爲「產品」的實體。不幸的是我沒有任何想法如何完成這項工作..

+0

你應該在查詢 – JimL

回答

1

在你的控制器,你的視圖餵養你需要排序的實體;

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

請看這裏;

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

的EntityRepository#findBy()方法還接受排序, 限制,作爲第二偏移到第四個參數:

我回答這個問題很不客氣地,因爲你需要提供更多你的實體和'Locatie'關聯的細節(entity.getLocatie()。getLocatienaam())

+0

我想你的答案,現在的工作實體排序排序。我看着錯誤的角落,而不是看着控制器,我認爲我不得不在我看來添加一些代碼。 – Gijsberts

相關問題