简体   繁体   English

使用Symfony上的Sonata Admin Bundle进行批处理操作

[英]Batch actions with Sonata Admin Bundle on Symfony

I need to get data grid on the controller but it does not allow to enter parameters in the function. 我需要在控制器上获取数据网格,但不允许在函数中输入参数。 How you recover that information? 你如何恢复这些信息?

services:
        admin.category:
                class: AppBundle\Admin\CategoryAdmin
                arguments: [~, AppBundle\Entity\Category, AppBundle:CRUDCategory, ~]            
                tags:
                    - { name: sonata.admin, manager_type: orm, group: "General", label: Categories }

This is the controller 这是控制器

<?php

namespace AppBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as SonataController;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery as ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class CRUDCategoryController extends SonataController {

    /**
     * @param ProxyQueryInterface $selectedModelQuery
     * @param Request             $request
     *
     * @return RedirectResponse
     */
    public function batchActionInactive(ProxyQueryInterface $selectedModelQuery, Request $request) {        
        $em = $this->getDoctrine()->getManager();
        $category = $em->getRepository('AppBundle:Category')->find($request->getId());
        $category->setState('INACTIVE');
        $em->flush();
        return new RedirectResponse(
            $this->admin->generateUrl('list', $this->admin->getFilterParameters())
        );
    }

}

And this is the function getBatchActions 这是函数getBatchActions

   public function getBatchActions() {
        $actions = parent::getBatchActions();
        unset($actions['delete']);
        $actions['inactive'] = array(
          'label' => 'Disable category',
          'ask_confirmation' => false
        );
        return $actions;
    }

The error is 错误是

Catchable Fatal Error: Argument 2 passed to AppBundle\\Controller\\CRUDCategoryController::batchActionInactive() must be an instance of AppBundle\\Controller\\Request, none given 可捕获的致命错误:传递给AppBundle \\ Controller \\ CRUDCategoryController :: batchActionInactive()的参数2必须是AppBundle \\ Controller \\ Request的实例,没有给出

It is much easier, do it this way instead of getting the categories by your own: 这样做更容易,而不是通过自己的方式获取类别:

/**
 * @param ProxyQueryInterface $selectedModelQuery
 *
 * @return RedirectResponse
 */
public function batchActionInactive(ProxyQueryInterface $selectedModelQuery)
{
    $selectedCategories = $selectedModelQuery->execute();

    try {
        /** @var Category $category */
        foreach ($selectedCategories as $category) {
            $category->setState('INACTIVE');
            $this->admin->update($category);
        }
    } catch (\Exception $e) {
        $this->addFlash(
            'sonata_flash_error',
            'Could not mark Categories "INACTIVE"'
        );

        $this->get('logger')->error($e->getMessage());

        return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
    }

    $this->addFlash(
        'sonata_flash_success',
        'Categories were marked as "INACTIVE"'
    );

    return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}

This has nothing to do with Sonata and everything to do with Symfony : you forgot the use statement for Request . 这与Sonata无关,与Symfony有关:你忘记了Request的使用声明。 The AppBundle\\Controller\\Request should have made you realize that ;) AppBundle\\Controller\\Request应该让你意识到;)

EDIT: second error : action methods should end with **Action* 编辑:第二个错误:动作方法应以**动作*结束

I've already solved. 我已经解决了。

public function batchActionInactive(ProxyQueryInterface $selectedModelQuery) {
    $em = $this->getDoctrine()->getManager();
    $request = $this->getRequest();
    $ids = $request->request->get('idx');
    foreach ($ids as $id) {
        $category = $em->getRepository('AppBundle:Category')->find($id);
        $category->setState('INACTIVE');
        $em->flush();
    }
    return new RedirectResponse(
        $this->admin->generateUrl('list', $this->admin->getFilterParameters())
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM