就像在《如何处理带有500多个项目的Symfony表单集合》中一样,我有一个大的Symfoy表单集合。在上面提到的线程中,有人建议对集合使用分页。
如何使用knpPaginatorBundle对这样的表单集合进行分页?
表单类型类似于此
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('myType', 'collection', array(
'type' => new MyType(),
))
;
}
我的控制器生成一个带有集合的表单
$form = $this->createForm(new MyFormCollectionType());
然后我尝试使用分页器
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$form['myType'],
$request->get('page', 1),
10
);
我想我使用了错误的分页目标($target),符号形式集合中正确的目标是什么?
Symfony3:示例
目录控制器
// I want to paginate products (my collection)
$products = $catalog->getProducts(); // ArrayCollection
// Pagination
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($products, $request->query->getInt('page', 1), 10);
// build the form with catalog data
$form = $this->createForm(CatalogProductType::class, $catalog, array("pagination" => $pagination));
// [...]
// show page
return $this->render('catalogs/products.html.twig', array(
'catalog' => $catalog,
'form' => $form->createView(),
'pagination' => $pagination
));
目录表单(CatalogProductType)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('products', CollectionType::class, array(
'entry_type' => ProductType::class,
'data' => $options['pagination'],
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label' => false,
'by_reference' => false
))
->add('save', SubmitType::class, array(
'attr' => array('class' => 'button-link save'),
'label' => 'Validate'
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundleEntityCatalog',
'pagination' => null // Don't forget this !
));
}
我的视图(products.html.twitch)
<div class="navigation">{{ knp_pagination_render(pagination) }}</div>
{% if pagination.getTotalItemCount > 0 %}
{% for widget in form.products.children %}
{# Refers to a Twig macro #}
{{ _self.prototype_product(widget) }}
{% endfor %}
{% else %}
<div class="error">The is no product in this catalog</div>
{% endif %}
我的产品收藏现在已分页。
解决方案:查询集合的所有实体,对这些实体进行分页,并将集合的数据属性设置为分页值。
控制器:
$allEntities = $em->getRepository('MyBundle:Entity')->findAll();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$allEntities,
$request->get('page', 1),
10
);
$form = $this->createForm(new MyFormCollectionType($pagination));
表单类型:
private $data;
public function __construct($data) {
$this->data = $data;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('myType', 'collection', array(
'type' => new MyType(),
'data' => $this->data,
))
;
}