在Symfony2中筛选列表? [英] Filter a listing in Symfony2?

查看:141
本文介绍了在Symfony2中筛选列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我尝试了Symfony,我是一个很新手。
我正在寻找一种优雅的方式来过滤列表。



让我解释一下:



我有两个实体:链接和标签。他们有多重关系。



在我的索引视图中,我创建了这个表单。我做了一个findAll()来获取所有我的标签的选择:

 < form method =GETaction = > 
< input class =btn btn-defaulttype =submit/>
< select name =tags []class =selectpickermultiple =yes>
{%标签中的标签%}
< option value ={{tag.id}}> {{tag.title}}< / option>
{%endfor%}
< / select>
< / form>

这是我通过DESC获取所有链接顺序的方式:

  $ links = $ em-> getRepository('TestDefaultBundle:Link') - > findBy(
array(),
array 'id'=>'desc')
);

如何收集选定的标签(在控制器中),并通过这些选定的标签获取所有链接过滤器



另一个问题我知道我们可以为一个实体生成一个表单,但是这种形式呢?



编辑



这是我的indexAction:

  public function indexAction(Request $ request)
{
$ em = $ this-> getDoctrine() - > getManager();
$ tags = $ em-> getRepository('LanCrmBundle:LinkTag') - > findAll();

//创建过滤器窗体。
$ form = $ this-> createFormBuilder()
- > add('tags','entity',array(
'class'=>'LanCrmBundle:LinkTag'
'multiple'=> true,
'expanded'=> false,
'query_builder'=> function(EntityRepository $ er){
return $ er-> ; createQueryBuilder('u')
- > orderBy('u.title','ASC');
}
))
- > add('OK' ,'submit')
- > getForm()
;

$ form-> handleRequest($ request);

if($ form-> isValid()){
$ data = $ form-> getData();

//获取标签过滤的所有链接。
//如何使用$数据过滤我的链接?
$ links = $ em-> getRepository('LanCrmBundle:Link') - > findBy(
array(),
数组('id'=>'desc')
);
} else {
//获取所有链接。
$ links = $ em-> getRepository('LanCrmBundle:Link') - > findBy(
array(),
数组('id'=>'desc')
);
}

$ paginator = $ this-> get('knp_paginator');
$ pagination = $ paginator-> paginate(
$ links,
$ this-> get('request') - > query-> get('page',1 ),
4
);

return $ this-> render('LanCrmBundle:Link:index.html.twig',array(
'pagination'=> $ pagination,
'tags' => $ tags,
'form'=> $ form-> createView()
));
}

我有这个错误:



在传递给选择字段的类型Lan\CrmBundle\Entity\LinkTag的对象上找不到__toString()方法。要读取一个自定义的getter,请将选项property设置为所需的属性路径。



StringCastException:在对象的对象上找不到__toString()方法键入Lan\CrmBundle\Entity\LinkTag,传递给选择字段。为了阅读一个自定义的getter,将选项property设置为所需的属性路径。

解决方案

其实最好的做法是cerate类型类。一个很好的捆绑过滤器是
https://github.com/lexik/LexikFormFilterBundle



而且我创建了一个typeGuesser Bundle,它使用lexik的类型创建一个以您的EntityFormType类为参数的filterForm。
https://github.com/juanmf/FilterTypeGuesserBundle
安装了两个软件包,和类型类,代码减少到过滤器和一个方法(我在README.md中留下了一个示例)来创建查询。

  private function createFilterForm($ docType)
{
$ adapter = $ this-> get('dd_form.form_adapter');
$ type = $ this-> getFormForDocument($ this-> getClassFromDocType($ docType));
return $ adapter-> adaptForm(
$ type,
$ this-> generateUrl('document_search',array('docType'=> $ docType)),
array('pdfPath','pdfPages','batchStatus','createdInBatch','documentType')
);
}

注意:从Sf> = 2.8 formTypes更改为FQCN,所以typeGuesser找不到类型名称。需要修复。


Hello I tried Symfony and I'm a very novice. I am looking for an elegant way to filter listings.

Let me explain:

I have two entities: Link and Tag. They are in multiple relation.

On my index view I created this form. I did a findAll() to get all my tags for the select:

<form method="GET" action="">
    <input class="btn btn-default" type="submit"/>
    <select name="tags[]" class="selectpicker" multiple="yes">
        {% for tag in tags %}
            <option value="{{ tag.id }}"> {{ tag.title }}</option>
        {% endfor %}
    </select>
</form>

This is the way I grab all link order by DESC:

$links = $em->getRepository('TestDefaultBundle:Link')->findBy(
    array(),
    array('id' => 'desc')
);

How can I collect the selected tags (in the controller) and grab all links filter by these selected tags.

Another question I know we can generate a form for an entity but what about this kind of form?

EDIT

This is my indexAction:

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $tags = $em->getRepository('LanCrmBundle:LinkTag')->findAll();

    // Create the filter form.
    $form = $this->createFormBuilder()
        ->add('tags', 'entity', array(
            'class' => 'LanCrmBundle:LinkTag',
            'multiple' => true,
            'expanded' => false,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.title', 'ASC');
            }
        ))
        ->add('OK', 'submit')
        ->getForm()
    ;

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

        // Get all links filtered by tags.
        // How to use the $data to filter my links?
        $links = $em->getRepository('LanCrmBundle:Link')->findBy(
            array(),
            array('id' => 'desc')
        );
    } else {
        // Get all links.
        $links = $em->getRepository('LanCrmBundle:Link')->findBy(
            array(),
            array('id' => 'desc')
        );
    }

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $links,
        $this->get('request')->query->get('page', 1),
        4
    );

    return $this->render('LanCrmBundle:Link:index.html.twig', array(
        'pagination' => $pagination,
        'tags' => $tags,
        'form' => $form->createView()
    ));
}

I've got this error :

A "__toString()" method was not found on the objects of type "Lan\CrmBundle\Entity\LinkTag" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.

StringCastException: A "__toString()" method was not found on the objects of type "Lan\CrmBundle\Entity\LinkTag" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.

解决方案

Actually the best practice is to cerate Type classes. a great bundle to make filters is https://github.com/lexik/LexikFormFilterBundle

And I created a typeGuesser Bundle that uses lexik's types for creating a filterForm with your EntityFormType class as parameter. https://github.com/juanmf/FilterTypeGuesserBundle With both bundles installed, and your Type Class in place, the code reduces to this for the filter and a method (I left an example in the README.md) for creating the Query.

    private function createFilterForm($docType)
    {
        $adapter = $this->get('dd_form.form_adapter');
        $type = $this->getFormForDocument($this->getClassFromDocType($docType));
        return $adapter->adaptForm(
            $type,
            $this->generateUrl('document_search', array('docType' => $docType)),
            array('pdfPath', 'pdfPages', 'batchStatus', 'createdInBatch', 'documentType')
            );
    }

Note: as of Sf >= 2.8 formTypes changes to FQCN, so the typeGuesser can't find type names. Fix needed.

这篇关于在Symfony2中筛选列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆