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