phpsymfonysymfony4formcollectionknppaginatorbundle

FormCollection and KnpPaginator Symfony 4


Privet all! I'm new to Symfony 4 and generally to PHP and OOP in general. I was helped to make the form using FormCollection the code looks so

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()
        ]);
    }

and screenshot enter image description here

you see that my record with the form has both tags with a choiceType and a simple input

And I also figured out myself with knp_paginator, because if I output more than 15 records with FormCollection, I get an error about the lack of memory in php. I made a static table where I output all my records, sorting and filtering works, pagination is broken as it should. here is the code

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);
    }
}

and controller

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,
        ]);
    }

here screenshot enter image description here

Work a perfect!!

I bet I can not figure out how to make the same result, but with FormCollection?

The second question, if it turns out to be pagination, will filtering and sorting work from knp_paginator?


Solution

  • ok, ok, something happened and it's not right for mine, but Pagination and sorting + filtering work only on <input> tags, inside <select> <option selected> var </ select> nothing works, but the solution is working

    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
            ]);
        }
    

    twig tempalte

    {% 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 %}
    

    and 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);
        }
    }
    

    if anyone can tell how to implement sorting within <select> <option selected> var </ select>, you can use <th> {{knp_pagination_sortable (paginator, 'idItem', 't.idItem')}} </ th> write please!