symfony – 在Sonata管理实体的show模板中添加自定义表单

我想在Sonata Admin show模板中生成一个小表单.到目前为止,我所做的是在自定义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',array('label' => 'Date Requested'))
        ->with('Order Details')
            ->add('orderRows',NULL,array('template' => 'AppBundle:PmodorderRow:orderrow_overview.html.twig'))
        ->end()
        ->with('Actions')
            ->add('actions',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>

尝试这个时我得到一个错误;

An exception has been thrown during the rendering of a template
(“There is no _sonata_admin defined for the controller
ApBundle\Controller\PmodorderCRUDController and the
current route “”) in
AppBundle:PmodorderAction:order_actions.html.twig at line 3.

我从documentation了解到我需要使用这个configureRoutes方法;

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

但我不能让它工作,我不知道如何渲染表单而不是简单的链接按钮.

有人可以帮我解决我的问题吗?

解决方法

SonataAdminBundle使用_sonata_admin(route)属性获取所需的管理实例($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,所以这是OrderAdmin类(Yaml)的Sonata服务定义:

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

现在,根据您自己的approveOrderAction(),您可以按以下方式呈现此操作:

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

只是你必须添加管理代码:’purchase_bundle.admin.order_admin’,它应该工作!

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些