Sonata Admin Bundle如何配置批处理操作

时间:2016-10-04 06:33:03

标签: symfony sonata-admin symfony-sonata sonata

如何在奏鸣曲Admin Bundle中创建自定义配置批处理操作。 当我选择批处理操作时,我需要使用选定的列表视图项来配置一些更改,然后应用批处理。

我解决了这个问题:

SonataAdmin
public function getBatchActions()
{

    $actions = parent::getBatchActions();

    // check user permissions
    $actions['change'] = [
        'label'            => $this->trans('action_group_change', [], 'admin'),
        'ask_confirmation' => false,
    ];

    return $actions;
}

//在控制器中

public function batchActionChange(ProxyQueryInterface $selectedModelQuery, Request $request = null)
{
    // !$this->admin->isGranted('EDIT')
    // TODO:  при необходимости
    //
    //
    $modelManager = $this->admin->getModelManager();
    $selectedModels = $selectedModelQuery->execute();


    $ids = [];

    foreach ($selectedModels as $selectedModel) {
        $ids[] = $selectedModel->getId();
    }


    return new RedirectResponse(
        $this->admin->generateUrl('change', [
            'ids' => $ids,
        ])
    );
}
public function changeAction(Request $request)
{

    $ids = $request->get('ids');
    if ($ids === null) {
        $this->addFlash('sonata_flash_error', 'Ничего не выбрано');

        return new RedirectResponse($this->admin->generateUrl('list'));
    }
    $form = $this->createForm(new ChangeProxyAdminType());

    if ($request->getMethod()==='POST'){
        $formHandler = $this->get('webface_character.handler.change_proxy');
        $handleResult =$formHandler->handle($form,$request);
        if ($handleResult){
            return new RedirectResponse($this->admin->generateUrl('list'));
        }
    }



    return $this->render('WebfaceCharacterBundle:ProxyAdmin:change.html.twig', [
        'action' => 'change',
        'form'   => $form->createView(),
    ]);

表格类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('user', EntityType::class, [
            'class'      => User::class,
            'empty_data' => null,
            'required'   => false,
        ])
        ->add('username', TextType::class, [
            'attr'     => [
                'class' => 'form-control',
            ],
            'required' => false,
        ])
        ->add('password', PasswordType::class, [
            'attr'     => [
                'class' => 'form-control',
            ],
            'required' => false,
        ])
        ->add('isArchive', CheckboxType::class, [
            'attr'     => [
                'class' => 'form-control',
            ],
            'required' => false,
        ]);

}

表单处理程序:

  public function handle(FormInterface $form, Request $request)
{
    if (!$request->isMethod('POST')) {
        return false;
    }

    $form->handleRequest($request);

    if (!$form->isValid()) {
        return false;
    }
    $data = $form->getData();

    $ids = $request->get('ids');
    $qb = $this->em->createQueryBuilder();
    $query = $qb->update('WebfaceCharacterBundle:Proxy', 'p');


    if ($data['user'] !== null) {
        $user = $this->em->getRepository('WebfaceUserBundle:User')->findOneBy(['id' => $data['user']]);
        dump($user);
        $query->set('p.user', $user->getId());
    }

    if ($data['password'] !== null) {
        $query->set('p.password', $qb->expr()->literal($data['password']));
    }

    if ($data['username'] !== null) {
        $query->set('p.username',$qb->expr()->literal($data['username']));
    }

    $query->set('p.isArchive', $qb->expr()->literal($data['isArchive']));
    $query->where($qb->expr()->in('p.id', implode(',', $ids)))
        ->getQuery()
        ->getResult();

    return true;


}

我确信这不是最佳解决方案。但我不知道如何为这个问题创造最佳解决方案。

此解决方案不适用于1000个项目的批处理操作,因为在我的解决方案中,所有ID都传递给查询和重定向。

P.S。 (这是草案)

1 个答案:

答案 0 :(得分:0)

如果要创建已配置的批处理操作:

  1. 列表项
  2. 创建自定义表单。
  3. 使用表单处理程序
  4. 创建Controller方法
  5. 在batchAction渲染表单中,其中action = controllerAction
  6. 表格处理后 返回新的RedirectResponse($ this-> admin-> generateUrl(' list'));