女贞!我是 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 工作?