FormCollection 和 KnpPaginator Symfony 4

FormCollection and KnpPaginator Symfony 4

女贞! 我是 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内存不足的错误。我做了一个静态 table 输出我所有的记录,排序和过滤工作,分页被破坏了。 这是代码

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开始工作吗?

好的, 好的,发生了一些事情,这对我来说不合适,但是分页和排序 + 过滤只对 <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>
    label{display: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);
    }
}

如果有人知道如何在 <select> <option selected> var </ select> 内实现排序,您可以使用 <th> {{knp_pagination_sortable (paginator, 'idItem', 't.idItem')}} </ th> 请写!