petkopara/multi-search-bundle

Symfony 扩展包,用于对 doctrine 实体进行多条件搜索,使用表单或服务。

v1.0.0 2016-10-24 20:35 UTC

This package is not auto-updated.

Last update: 2024-09-24 12:05:24 UTC


README

此扩展包提供了 Doctrine 多搜索的基本服务和表单类型。

Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Total Downloads

描述

通过给定搜索词在所有实体列中进行搜索。响应返回包含多个搜索条件的 Doctrine\ORM\QueryBuilder。可以指定搜索的列。

安装

使用 composer

composer require petkopara/multi-search-bundle

将其添加到 AppKernel.php 类中

new Petkopara\MultiSearchBundle\PetkoparaMultiSearchBundle(),

##使用方法

服务

您可以直接使用该服务并将多搜索应用于任何 doctrine 查询构建器。

public function indexAction(Request $request)
{
    $search = $request->get('search');
    $em = $this->getDoctrine()->getManager();
    
    $qb = $em->getRepository('AppBundle:Post')->createQueryBuilder('e');
    $qb = $this->get('petkopara_multi_search.builder')->searchEntity($qb, 'AppBundle:Post', $search);
   //$qb = $this->get('petkopara_multi_search.builder')->searchEntity($qb, 'AppBundle:Post', $search, array('name', 'content'), 'wildcard');

    ..
}

表单

创建您的表单类型,并在 buildForm 函数中包含 multiSearchType

use Petkopara\MultiSearchBundle\Form\Type\MultiSearchType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('search', MultiSearchType::class, array(
                'class' => 'AppBundle:Post', //required
                'search_fields' => array( //optional, if it's empty it will search in the all entity columns
                    'name',
                    'content'
                 ), 
                 'search_comparison_type' => 'wildcard' //optional, what type of comparison to applied ('wildcard','starts_with', 'ends_with', 'equals')
                 
            ))
    ;
}

在控制器中添加对多搜索服务的调用

public function indexAction(Request $request)
{
    $search = $request->get('search');
    $em = $this->getDoctrine()->getManager();
    $queryBuilder = $em->getRepository('AppBundle:Post')->createQueryBuilder('e');
    $filterForm = $this->createForm('AppBundle\Form\PostFilterType');

    // Bind values from the request
    $filterForm->handleRequest($request);

    if ($filterForm->isValid()) {
        // Build the query from the given form object
        $queryBuilder = $this->get('petkopara_multi_search.builder')->searchForm($queryBuilder, $filterForm->get('search'));
    }
    
    ..
}

在视图中渲染表单

{{ form_rest(filterForm) }}

可用选项

提供类型有 2 个选项

  • search_fields - 将添加到搜索中的实体列数组。如果没有设置,则将在所有列中搜索

  • search_comparison_type - 如何比较搜索词。

    • wildcard - 等价于 %search% 类似搜索。

    • equals - 无通配符的类似操作符。如果搜索词包含 *,则可以使用 equals 与通配符一起使用。

    • starts_with - 等价于 %search% 类似搜索。

    • ends_with - 等价于 search% 类似搜索。

这些参数可以应用于服务,也可以应用于 searchEntity() 方法的第 4 和第 5 个参数

作者

Petko Petkov - petkopara@gmail.com

许可

MultiSearchBundle 根据 MIT 许可证授权。