2016-07-28 52 views
8

我想在奏鳴曲管理員演示模板中生成一個小窗體。我迄今爲止所做的是在自定義CRUD中爲該特定實體(訂單)創建功能,該功能從Sonata的默認CRUD擴展而來;在奏鳴曲管理實體的演示模板中添加自定義窗體

public function approveOrderAction($id = null) 
{ 
    $request = $this->getRequest(); 

    $id = $request->get($this->admin->getIdParameter()); 
    $order = $this->admin->getObject($id); 

    $approveForm = $this->createFormBuilder($order) 
     ->add('reqSecondApprover', 'checkbox', array('label' => 'Require second Approval', 'required' => false)) 
     ->add('secondApprover', 'choice', array('choices' => Crud::getWhatever(array('Developer')), 'required' => false)) 
     ->getForm(); 

    $approveForm->handleRequest($request); 

    if ($approveForm->isSubmitted() && $approveForm->isValid()) { 
     $secondApproval = $request->request->get('form'); 
     $approval = $approveForm->getData(); 

     if (isset($secondApproval['reqSecondApprover'])) { 
      $order->setStatus(PmodOrder::STATUS_PARTLY_APPROVED); 
     } else { 
      $order->setStatus(PmodOrder::STATUS_APPROVED); 
      $order->setSecondApprover(null); 
     } 

     $em->persist($approval); 
     $em->flush(); 

     return new RedirectResponse($this->admin->generateUrl('show')); 
    } 

    return $this->render('AppBundle:PmodOrder:order_approve.html.twig', array(
     'order' => $order, 
     'form' => $approveForm->createView(), 
    )); 
} 

在我的訂單中我有configShowFields方法;

protected function configureShowFields(ShowMapper $showMapper) 
{ 
    $order = $this->getSubject(); 

    $showMapper 
     ->with('General') 
      ->add('createdBy', null, array('label' => 'Requested By')) 
      ->add('createdAt', null, array('label' => 'Date Requested')) 
     ->with('Order Details') 
      ->add('orderRows', NULL, array('template' => 'AppBundle:PmodOrderRow:orderrow_overview.html.twig')) 
     ->end() 
     ->with('Actions') 
      ->add('actions', NULL, array('template' => 'AppBundle:PmodOrderAction:order_actions.html.twig', 'route' => 'approve')) 
     ->end() 
    ; 
} 

order_actions模板看起來是這樣的,並會顯示相關的功能根據訂單的狀態,誰登錄了,所以怎麼有這麼多diffent路線工作?;

<td> 
    {% if app.user.id == object.firstApprover and object.status == 1%} 
     {{ render(controller('AppBundle:PmodOrderCRUD:approveOrder', { 'id': object.id })) }} 
    {% elseif app.user.id == object.secondApprover and object.status == 2 %} 
     <a href="{{ path('order_second_approve', { 'id': object.id })}}" class="btn btn-primary"><i class="fa fa-check"></i> Approve</a> 
     <a href="{{ path('order_disapprove', { 'id': object.id })}}" class="btn btn-default"><i class="fa fa-times"></i> Disapprove</a> 
    {% elseif app.user == object.createdBy and object.status == 3 %} 
     <a href="{{ path('order_place', { 'id': object.id })}}" class="btn btn-primary">Place Order</a> 
     <a href="{{ path('order_place', { 'id': object.id })}}" class="btn btn-default">Cancel Order</a> 
    {% else %} 
     - 
    {% endif %} 
</td> 

嘗試此操作時出現錯誤;

例外,已經模板 的呈現期間已經拋出(「沒有用於控制器 ApBundle\Controller\PmodOrderCRUDController和 當前路線``定義_sonata_admin」)在 的appbundle:PmodOrderAction:order_actions.html.twig在線3.

我明白從documentation,我需要使用此configureRoutes方法;

protected function configureRoutes(RouteCollection $collection) 
{ 
    $collection->add('clone', $this->getRouterIdParameter().'/clone'); 
} 

但我不能得到它的工作,我不知道如何呈現形式,而不是一個簡單的鏈接按鈕。

有人可以幫我解決我的問題嗎?

回答

2

_sonata_admin(路線)屬性由獲得所需要的管理實體($this->admin),並能夠配置/處理您的要求:

後加入正確的路由定義:

protected function configureRoutes(RouteCollection $collection) 
{ 
    $collection->add('approve_order', $this->getRouterIdParameter().'/approve'); 
} 

您還需要添加_sonata_admin代碼來生成正確的請求approveOrderAction()

{{ render(controller('QiBssFrontendBundle:PmodOrderCRUD:approveOrder', { 'id': object.id, '_sonata_admin': '...' })) }} 

讓我們做一個簡單的例子:

你有一個Order實體及其管理類:OrderAdminPurchaseBundle,所以這是OrderAdmin類(YAML)索納塔的服務定義:

services: 
    purchase_bundle.admin.order_admin: 
     class: PurchaseBundle\Admin\OrderAdmin 
     arguments: 
      - ~ 
      - PurchaseBundle\Entity\Order 
      - ~ 
     tags: 
      - { name: 'sonata.admin', manager_type: orm } 

現在,基於

{{ render(controller('PurchaseBundle:OrderAdmin:approveOrder', { 'id': object.id, '_sonata_admin': 'purchase_bundle.admin.order_admin' })) }} 

只要你需要添加管理員密碼:在您自己的approveOrderAction(),您可以在如下的方式呈現這個動作'purchase_bundle.admin.order_admin',它應該工作!

相關問題