【问题标题】:Adding a search box to filter a list of results in Symfony?添加一个搜索框来过滤 Symfony 中的结果列表?
【发布时间】:2011-07-30 17:02:27
【问题描述】:

作为 Symfony 中典型的 indexSuccess 操作的结果,我需要在对象列表中放置一个搜索框。目标很简单:根据条件过滤列表。

我一直在阅读Zend Lucene approach in Jobeet tutorial,但这似乎是用大锤敲碎坚果(至少对我的要求而言)。

我对自动生成的管理过滤器表单更感兴趣,但我不知道如何在前端实现它。

我可以简单地将搜索框内容传递给操作并构建自定义查询,但是有没有更好的方法来做到这一点?

编辑

我忘了提到我想要一个通用输入字段,而不是每个模型属性的输入字段。

谢谢!

【问题讨论】:

    标签: list search symfony1 filter doctrine


    【解决方案1】:

    我终于使用 Symfony 生成的默认 MyModuleForm 制作了一个自定义表单

    public function executeIndex {
        ...
        // Add a form to filter results
        $this->form = new MyModuleForm();
    }
    

    但只显示一个自定义字段:

    <div id="search_box">
        <input type="text" name="criteria" id="search_box_criteria" value="Search..." />
        <?php echo link_to('Search', '@my_module_search?criteria=') ?>
    </div>
    

    然后我创建了一个名为@my_module_search 的路由链接到索引操作:

    my_module_search:
      url:          my_module/search/:criteria
      param:        { module: my_module, action: index }
      requirements: { criteria: .* }  # Terms are optional, show all by default
    

    使用 Javascript(在本例中为 jQuery)我将输入的文本附加到链接的 href 属性中的条件参数:

    $('#search_box a').click(function(){
        $(this).attr('href', $(this).attr('href') + $(this).prev().val());
    });
    

    最后,回到executeIndex 操作,我检测是否输入了文本并将自定义过滤器添加到DoctrineQuery 对象:

    public function executeIndex {
        ...
        // Deal with search criteria
        if ( $text = $request->getParameter('criteria') ) {
            $query = $this->pager->getQuery()
                ->where("MyTable.name LIKE ?", "%$text%")
                ->orWhere("MyTable.remarks LIKE ?", "%$text%")
                ...;
        }
    
        $this->pager->setQuery($query);
    
        ...
        // Add a form to filter results
        $this->form = new MyModuleForm();
    }
    

    其实代码比较复杂,因为我在父类中写了一些partials和一些方法来复用代码。但这是我能想到的最好的了。

    【讨论】:

      【解决方案2】:

      我正在使用这个解决方案,而不是集成 Zend Lucene,我设法使用了自动生成的 Symonfy 过滤器。我就是这样做的:

      //module/actions.class.php
      public function executeIndex(sfWebRequest $request)
      {
            //set the form filter
            $this->searchForm = new EmployeeFormFilter();
            //bind it empty to fetch all data
            $this->searchForm->bind(array());
            //fetch all
            $this->employees = $this->searchForm->getQuery()->execute();
            ...
      }
      

      我做了一个搜索动作来进行搜索

      public function executeSearch(sfWebRequest $request)
      {
        //create filter
        $this->searchForm = new EmployeeFormFilter();
        //bind parameter
        $fields = $request->getParameter($this->searchForm->getName());
        //bind
        $this->searchForm->bind($fields);
        //set paginator
        $this->employees = $this->searchForm->getQuery()->execute();
        ...
        //template
        $this->setTemplate("index");
      }
      

      搜索表单转到 mymodule/search 操作很重要。

      实际上,我还使用sfDoctrinePager 直接分页设置表单生成的查询以获得正确分页的结果。

      如果您想在搜索表单中添加更多字段,请查看this :)

      【讨论】:

      • 这迫使我使用模型属性进行过滤,即为每个小部件使用不同的输入。我正在考虑拥有一个独特的输入字段并在模型列中使用 LIKE 构建自定义查询。
      • 那是一个完全不同的答案......您必须对每个字段进行迭代并进行正确的查询。稍后我会检查我是否可以做到。
      • 我知道 :) 感谢您的努力。我一直在挖掘后端缓存,试图提出基于生成类的替代解决方案。如果我发现了什么,我会更新我的答案
      猜你喜欢
      • 2019-07-17
      • 2018-07-26
      • 2015-09-28
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多