gales-petkopara / multi-search-bundle
Symfony 扩展包,用于对 doctrine 实体进行多条件搜索,支持使用表单或服务。
v2.0.4
2023-08-30 14:37 UTC
Requires
- doctrine/orm: ^2.5
- symfony/form: ^2.8|^3.0|^4.0|^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ~4.8
README
此扩展包提供了 Doctrine 多搜索的基本服务和表单类型。
描述
通过给定的搜索词在所有实体列中进行搜索。响应返回包含多个搜索条件的 Doctrine\ORM\QueryBuilder
。可以指定搜索的列。
安装
使用 composer
composer require gales-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
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 许可下授权。