简体   繁体   English

Symfony4 / Doctrine2上具有可变数量标准的表格

[英]Form with variable number of criterias on Symfony4 / Doctrine2

Let's take https://symfony.com/doc/current/forms.html#building-the-form example form but only for Search in tasks list instead of save. 让我们以https://symfony.com/doc/current/forms.html#building-the-form示例表单为例,但仅用于任务列表中的搜索而不是保存。

Goal is to allow searches on task, dueDate or both criterias (in my real case, I have 9 criterias) 目标是允许搜索任务,DueDate或两个条件(在我的实际情况下,我有9个条件)

Here are src/Repository/ResultRepository.php : 这是src / Repository / ResultRepository.php

class ResultRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Result::class);
    }

    public function findMultiKeys($task, $dueDate): array
    {
        $qb = $this->createQueryBuilder('d')
            ->andWhere('d.task = :task')
            ->setParameter('task', $task)
            ->andWhere('d.dueDate = :dueDate')
            ->setParameter('dueDate', $dueDate)
            ->getQuery();

        return $qb->execute();
    }   
}

It require both criterias to return result(s)! 它需要两个条件才能返回结果!

I ran a lot of searches and find: 我进行了很多搜索,发现:

So I code a $where_string variable to construct my variable where. 因此,我编写了$ where_string变量以在哪里构造变量。 and a $parameters to construct my variable parameters array, and my findMultiKeys becomes: $ parameters构造我的可变参数数组,我的findMultiKeys变成:

public function findMultiKeys($get_form): array
{
    $where_string = '';
    $parameters = [];
    $task = $get_form->getTask();
    if ($task !== null) {
        $where_string .= "d.task = :task";
        $parameters += array('task' => $task);
    }
    $dueDate = $get_form->getDueDate();
    if ($dueDate !== null) {
        if ($where_string !== '')
            $where_string .= " AND ";
        $where_string .= "d.dueDate = :dueDate";
        $parameters += array('dueDate' => $dueDate);
    }

    $qb = $this->createQueryBuilder('d')
        ->where($where_string)
        ->setParameters($parameters)
        ->getQuery();

    return $qb->execute();
}   

It works, perhaps not the best way? 它可行,也许不是最好的方法? In my searches, I found, of course, to use ElasticSearch , perhaps to much to my simple need, or I found PetkoparaMultiSearchBundle 在搜索中,我发现当然可以使用ElasticSearch ,或者满足我的简单需求,或者我找到了PetkoparaMultiSearchBundle

Bests solutions welcomes 最佳解决方案欢迎

I have a proposition :-) 我有一个建议:-)

  1. You can build an task4Seach entity with your 9 criteria (without persistance) 您可以使用9个条件(无需持久性)构建task4Seach实体
  2. create a form type with this "data_class" 使用此“ data_class”创建表单类型

  3. use it to get your datas in your repository 用它来获取数据到仓库中

 $qb = $this->createQueryBuilder('d'); ... if ($task4Seach ->getDueDate()) { $qb->andWhere($qb->expr()->eq('d.dueDate', ':dueDate')); $qb->setParameter('dueDate', $task4Seach->getDueDate()); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Doctrine2(Symfony4)数据库连接url over socket - Doctrine2 (Symfony4) database connection url over socket Symfony4:Doctrine2工作但PHPUnit测试中没有连接(内核启动) - Symfony4: Doctrine2 works but no connection in PHPUnit test (kernel booted) 在持久之前将表单数据绑定到变量-Symfony2 / Doctrine2 - Bind form data to variable before persist - Symfony2 / Doctrine2 使用 symfony4、doctrine2 和 stofDoctrineExtensionsBundle 按任意字段对 Gedmo 树进行排序 - Sort Gedmo Tree by arbitrary field using symfony4, doctrine2 and stofDoctrineExtensionsBundle Symfony-doctrine2中的子查询 - Symfony - Subquery in doctrine2 Symfony2 Doctrine2 Oracle数据库NUMBER DEFAULT类型? - Symfony2 Doctrine2 Oracle database NUMBER DEFAULT type? Symfony2,Doctrine2和PostgreSQL:错误“未定义的变量:className” - Symfony2, Doctrine2 and PostgreSQL: error “Undefined variable: className” Symfony3 Doctrine2无法访问循环中的变量 - Symfony3 Doctrine2 can not access variable in the loop Symfony2和Doctrine2:使用两个实体填充表单(一个复杂的场景) - Symfony2 and Doctrine2 : populate form with two Entity (a complicated scenario) Symfony 3.2 Doctrine2 ORM以类的形式存储复杂查询 - Symfony 3.2 Doctrine2 ORM storage of complex queries in the form of classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM