我想在Sonata Admin节目模板中生成一个小表单。到目前为止,我所做的是在自定义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(),
));
}
在我的orderAdmin中,我有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
模板如下所示,将根据订单状态和登录者显示相关功能,因此如何处理这么多不同的路线?;
<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>
当我尝试这个时,我得到了一个错误;
在呈现模板期间引发异常("没有为控制器定义
_sonata_admin
CCD_ 4和当前路由``")AppBundle:PmodOrderAction:order_actions.html.twig,第3行。
我从文件中了解到,我需要使用这种configureRoutes
方法;
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('clone', $this->getRouterIdParameter().'/clone');
}
但我无法让它发挥作用,我也不确定如何呈现表单而不是简单的链接按钮。
有人能帮我解决问题吗?
SonataAdminBundle
使用_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
实体及其管理类:OrderAdmin
到PurchaseBundle
,所以这是Sonata对OrderAdmin
类(Yaml)的服务定义:
services:
purchase_bundle.admin.order_admin:
class: PurchaseBundleAdminOrderAdmin
arguments:
- ~
- PurchaseBundleEntityOrder
- ~
tags:
- { name: 'sonata.admin', manager_type: orm }
现在,基于您自己的approveOrderAction()
,您可以按照以下方式呈现此操作:
{{ render(controller('PurchaseBundle:OrderAdmin:approveOrder', { 'id': object.id, '_sonata_admin': 'purchase_bundle.admin.order_admin' })) }}
只需添加管理代码:'purchase_bundle.admin.order_admin'
,它就可以工作了!