1

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

能力控制器.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 内存不足的错误。我制作了一个静态表,在其中输出所有记录,排序和过滤工作,分页被破坏了。这是代码

服务文件.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 工作?

4

1 回答 1

0

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

控制器文件.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> write please!

于 2018-09-10T17:06:20.137 回答