2016-09-17 72 views
1

我需要在控制器上獲取數據網格,但不允許在該函數中輸入參數。你如何恢復這些信息?使用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

回答

2

這是很容易,做這種方式,而不是由你自己得到的類別:

/** 
* @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())); 
} 
1

這有什麼好做索納塔和一切與Symf做ony:你忘記了Request的使用聲明。該AppBundle\Controller\Request應該讓你知道;)

編輯:第二個錯誤:操作方法應與**行動*

+0

謝謝,我已經添加了行'使用的Symfony \分量\ HttpFoundation \請求;' ,但問題仍然存在 – Dasaaf

+0

看到我的編輯,你沒有把「行動」放在正確的地方 – greg0ire

+0

我跟着這個文檔[鏈接](https://sonata-project.org/bundles/admin/master/doc/reference/ batch_actions.html#)雖然symfony處理,如你所說,奏鳴曲用'batchActionInactive'確實。 – Dasaaf

0

我已經解決了結束。

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()) 
    ); 
}