我需要在控制器上獲取數據網格,但不允許在該函數中輸入參數。你如何恢復這些信息?使用Symfony上的Sonata Admin Bundle執行批處理操作
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, AppBundle:CRUDCategory, ~]
tags:
- { name: sonata.admin, manager_type: orm, group: "General", label: Categories }
這是控制器
<?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())
);
}
}
這是函數getBatchActions
public function getBatchActions() {
$actions = parent::getBatchActions();
unset($actions['delete']);
$actions['inactive'] = array(
'label' => 'Disable category',
'ask_confirmation' => false
);
return $actions;
}
的錯誤是
Catchable Fatal Error: Argument 2 passed to AppBundle\Controller\CRUDCategoryController::batchActionInactive() must be an instance of AppBundle\Controller\Request, none given
謝謝,我已經添加了行'使用的Symfony \分量\ HttpFoundation \請求;' ,但問題仍然存在 – Dasaaf
看到我的編輯,你沒有把「行動」放在正確的地方 – greg0ire
我跟着這個文檔[鏈接](https://sonata-project.org/bundles/admin/master/doc/reference/ batch_actions.html#)雖然symfony處理,如你所說,奏鳴曲用'batchActionInactive'確實。 – Dasaaf