【问题标题】:Symfony2 - Propel and KnpPaginatorBundle: how to sort by column?Symfony2 - Propel 和 KnpPaginatorBundle:如何按列排序?
【发布时间】:2012-09-17 17:27:25
【问题描述】:

在使用 Propel ORM 的 Symfony2 项目中,我尝试使用 KNP Paginator 包按列对列表视图进行排序。我的代码如下:

public function indexAction() 
{
    $query = CustomerQuery::create();

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

    return compact('pagination');
}

我的看法是:

<table>
    <thead>
        <tr>
            <th>{{ pagination.sortable('Company', 'companyName')|raw }}</th>
            <th>{{ pagination.sortable('Contact', 'contact')|raw }}</th>
            <th>{{ pagination.sortable('E-mail', 'email')|raw }}</th>
        </tr>
    </thead>
    <tbody>
        {% for customer in pagination %}
            <!-- [...] -->
        {% endfor %}
    </tbody>
</table>

生成的链接似乎是正确的,正如我得到的那样(例如):

/customers/?sort=companyName&direction=asc&page=1

但它不会对我的查询进行排序:在我生成的请求中没有 ORDER BY 子句。所以,我的问题是:Propel 是否可以进行结果排序(如果可以,我错在哪里)?还是应该在控制器中手动处理?

【问题讨论】:

    标签: symfony propel


    【解决方案1】:

    您可以为 *knp_paginator.subscriber* 事件创建一个订阅者,并使其成为对查询应用某种排序的类。

    Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\QuerySubscriber 为例

    我将对 knp_components 进行 PR 以添加 PropelQuerySortable 类,但我不知道确切的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2014-11-27
      • 1970-01-01
      • 2013-01-24
      • 2013-12-27
      相关资源
      最近更新 更多