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()));
}
This has nothing to do with Sonata and everything to do with Symfony : you forgot the use statement for Request
. The AppBundle\\Controller\\Request
should have made you realize that ;)
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())
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.