FormCollection 和 KnpPaginator Symfony 4

Posted

技术标签:

【中文标题】FormCollection 和 KnpPaginator Symfony 4【英文标题】:FormCollection and KnpPaginator Symfony 4 【发布时间】:2018-09-09 19:09:42 【问题描述】:

女贞! 我是 Symfony 4 的新手,通常是 php 和 OOP 的新手。我被帮助使用 FormCollection 制作表单,代码看起来如此

AbilityContoller.php

/**
     * @Route("/edit", name="ability_edit_all")
     */
    public function edit(Request $request)
    
        $abilitys = $this->getDoctrine()->getRepository(Ability::class)->findAll();
        $form = $this->createForm(AbilityArrayType::class, ['abilitys' => $abilitys]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) 
            foreach ($form->getData()['abilitys'] as $ability)
                $this->getDoctrine()->getManager()->persist($ability);
            
            $this->getDoctrine()->getManager()->flush();

            $this->addFlash('notice', 'Your changes were saved!');

            return $this->redirectToRoute('ability_edit_all');
        
        return $this->render('ability/edit.all.html.twig', [
            'form' => $form->createView()
        ]);
    

和截图

您会看到我的表单记录包含带有choiceType 和简单输入的标签

我还用 knp_paginator 弄清楚了自己,因为如果我用 FormCollection 输出超过 15 条记录,我会收到一个关于 php 内存不足的错误。我制作了一个静态表,在其中输出所有记录,排序和过滤工作,分页被破坏了。 这是代码

ServiceFile.php

class AbilityService
    protected $em;
    protected $container;

    public function __construct(EntityManagerInterface $entityManager, ContainerInterface $container)
    
        $this->em = $entityManager;
        $this->container = $container;
    

    public function ReturnData($request)
        $em = $this->em;
        $container = $this->container;
        $query = $em->createQuery(
            '
            SELECT 
                t.idItem,
                t.code,
                t.reloadTime,
                t.durationTime,
                t.idAbilityType
            FROM 
                App\Entity\Ability t
            '
        );
        dump($query);
        //$result = $query->execute();
        $pagenator = $container->get('knp_paginator');
        $results = $pagenator->paginate(
            $query,
            $request->query->getInt('page', 1),
            $request->query->getInt('limit', 10)
        );
        return ($results);
    

和控制器

use App\Service\ServiceFile;
/**
     * @Route("/test-edit", name="ability_test_edit_all")
     */
    public function edit_test(AbilityService $query, Request $request)
    
        $data = $query->ReturnData($request);

        return $this->render('ability/edit.alltest.html.twig',[
            'form' => $data,
        ]);
    

这里截图

工作完美!!

我敢打赌,我不知道如何获得相同的结果,但使用 FormCollection?

第二个问题,如果是分页,过滤和排序会从knp_paginator工作吗?

【问题讨论】:

将部分能力传递给 CreateForm。 【参考方案1】:

好的, 好的,发生了一些事情,它不适合我,但分页和排序 + 过滤仅适用于 <input> 标记,在 <select> <option selected> var </ select> 内没有任何作用,但解决方案有效

controllerFile.php

/**
     * @Route("/edit", name="ability_edit_all")
     */
    public function edit(AbilityService $abilityService, Request $request)
    
        $data = $abilityService->ReturnData($request);
        $form = $this->createForm(AbilityArrayType::class, ['abilitys' => $data->getItems()]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) 
            foreach ($form->getData()['abilitys'] as $properties)
                dump($properties);
                $ability = $this->getDoctrine()->getRepository(Ability::class)->find($properties['id']);
                $ability->setIdItem($properties['idItem']);
                $ability->setCode($properties['code']);
                $ability->setReloadTime($properties['reloadTime']);
                $ability->setDurationTime($properties['durationTime']);
                $ability->setIdAbilityType($properties['idAbilityType']);
                dump($ability);
                $this->getDoctrine()->getManager()->persist($ability);
            
            $this->getDoctrine()->getManager()->flush();

            $this->addFlash('notice', 'Your changes were saved!');//проделать со всеми см edit.html.twig

            return $this->redirectToRoute('ability_edit_all');
        
        return $this->render('ability/edit.all.html.twig', [
            'form' => $form->createView(),
            'paginator' => $data
        ]);
    

树枝模板

% extends 'base.html.twig' %
% block title %Ability edit all% endblock %

% block body %
<style>
    labeldisplay:none
</style>
    <h1>Ability edit all</h1>
    <a class="btn-add mar-bot-top-20" href=" path('ability_new') "><i class="fas fa-plus"></i> Create new</a>

    <div class="row">
        <div class="col-4 text-center">
            <div class="navigation">
                 knp_pagination_render(paginator) 
            </div>
        </div>
        <div class="col-8 text-left">
            <div class="filtration">
                 knp_pagination_filter(paginator, 
                    't.idItem': 'IdItem',
                    't.code': 'Code',
                    't.reloadTime': 'ReloadTime',
                    't.durationTime': 'DurationTime',
                    't.idAbilityType': 'IdAbilityType',
                ) 
            </div>
        </div>
    </div>

    <table class="table">
        <tr>
            <th> knp_pagination_sortable(paginator, 'idItem', 't.idItem') </th>
            <th> knp_pagination_sortable(paginator, 'Code', 't.code') </th>
            <th> knp_pagination_sortable(paginator, 'ReloadTime', 't.reloadTime') </th>
            <th> knp_pagination_sortable(paginator, 'DurationTime', 't.durationTime') </th>
            <th> knp_pagination_sortable(paginator, 'idAbilityType', 't.idAbilityType') </th>
        </tr>
         form_start(form) 
        % for ability in form.abilitys %
        <tr>
            <td> form_row(ability.idItem) </td>
            <td> form_row(ability.code) </td>
            <td> form_row(ability.reloadTime) </td>
            <td> form_row(ability.durationTime) </td>
            <td> form_row(ability.idAbilityType) </td>
        </tr>
        % else %
        <tr>
            <td colspan="9">no records found</td>
        </tr>
        % endfor %
         form_end(form) 
    </table>
    <div class="row">
        <div class="col-4 text-center">
            <div class="navigation">
                 knp_pagination_render(paginator) 
            </div>
        </div>
        <div class="col-8 text-left">
            <div class="filtration">
                 knp_pagination_filter(paginator, 
                    't.idItem': 'IdItem',
                    't.code': 'Code',
                    't.reloadTime': 'ReloadTime',
                    't.durationTime': 'DurationTime',
                    't.idAbilityType': 'IdAbilityType',
                ) 
            </div>
        </div>
    </div>

    <a class="btn-add" href=" path('ability_new') "><i class="fas fa-plus"></i> Create new</a>
    % for message in app.flashes('notice') %
        <div class="flash-notice">
             message 
        </div>
    % endfor %  

% endblock %

ServiceFile.php

<?php
namespace App\Service;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Ability;

class AbilityService
    protected $em;
    protected $container;

    public function __construct(EntityManagerInterface $entityManager, ContainerInterface $container)
    
        $this->em = $entityManager;
        $this->container = $container;
    

    public function ReturnData($request)
        $em = $this->em;
        $container = $this->container;
        $query = $em->createQuery(
            '
            SELECT 
                t.id,
                t.idItem,
                t.code,
                t.reloadTime,
                t.durationTime,
                t.idAbilityType,
                t.dateCreated,
                t.author,
                t.dateChange,
                t.lastAuthor
            FROM 
                App\Entity\Ability t
            '
        );
        $pagenator = $container->get('knp_paginator');
        $results = $pagenator->paginate(
            $query,
            $request->query->getInt('page', 1),
            $request->query->getInt('limit', 6)
        );
        return ($results);
    

如果有人知道如何在&lt;select&gt; &lt;option selected&gt; var &lt;/ select&gt; 中实现排序,您可以使用&lt;th&gt; knp_pagination_sortable (paginator, 'idItem', 't.idItem') &lt;/ th&gt; 请写!

【讨论】:

一切都是通过改变请求来决定的

以上是关于FormCollection 和 KnpPaginator Symfony 4的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 AJAX JQuery ASP .NET MVC 4 将 FormCollection 和文件传递给控制器

如何从FormCollection返回对象列表?

使用 FormCollection

ASP.NET 移动视图中的 FormCollection 为空

MVC4 中的 FormCollection 与 Html Actionlink

处理FormCollection字段的帮助类