Symfony2

时间:2015-09-11 14:28:30

标签: forms symfony pagination twig

我必须处理一个搜索表单,在Symfony2中处理多个输入 - fiels和分页。

因此我创建了一条路线

/**
  * @Route("/project/{page}/{search}", defaults={"page" = 1, "search" = "all", })
  **/

没有内容的搜索是默认的"所有" - 否则它是一个数组。页面包含用户当前显示的页面数。

控制器:

public function showAll($page, $search, Request $request)
{
    if ($search != 'all') {
        $search = unserialize($search);
        if(count($search)) {
            foreach ($search as $key => $value) {
                if ($key == 'name') {
                    $searchProject->setName($value);
                }
                .....
            }
        }
    }
    $searchForm = $this->createForm(new ProjectSearchType(), $searchProject);
    $searchForm->handleRequest($request);

    if ($searchForm->isSubmitted()) {
        $name = $searchForm["name"]->getData();
        $id = $searchForm["id"]->getData();
        $search = array('name' => $name, 'id' => $id);
    }

    $repository = $this->getDoctrine()->getRepository('AppBundle:Project');
    if ($search == 'all') {
        $projectResult = $repository->getAllActiveEntries($page, $itemsPerPage);
    }
    else {
        $projectResult = $repository->getAllActiveEntries($page, $itemsPerPage, $search);
    }

    $projectTable = $projectResult["query"];
    $totalCount = $projectResult["total"];

    $pagination = new \lib\Pagination($page, $totalCount, $itemsPerPage);
    $pageList = $pagination->getPagesList();
    $route_params = $request->attributes->get('_route_params');
    $route_params["search"] = serialize($search);

    return $this->render(
        'project/overview.html.twig',
        array(
            'searchForm' => $searchForm->createView(),
            'table' => $projectTable,
            'pagination' => $pageList,
            'currentPage' => $page,
            'currentFilters' => $route_params,
            'lastPage' => $pagination->getTotalPages(),
            'totalcount' => $totalCount,
            'paginationPath' => $route,
            'showAlwaysFirstAndLast' => true
        )
    );

DB-工作:

class ProjektRepository extends EntityRepository
{
    public function getAllActiveEntries($page, $rowsPerPage, $search=array())
    {
        $startFromEntry = ($page-1)*$rowsPerPage;

        $qb = $this->createQueryBuilder('project');
        if((count($search)) && (is_array($search))) {
            foreach ($search as $key => $value) {
                if ($value != '') {
                    $qb->andWhere('project.' . $key . ' LIKE :' . $key)
                        ->setParameter($key, $value);
                }
            }
        }

        $q = $qb;

        $countQuery = $q;
        $totalCount = $countQuery->select('COUNT(project)')
            ->getQuery()->getSingleScalarResult();

        $selectQuery = $q;
        $selectResult = $selectQuery->select(array('project'))
            ->setFirstResult($startFromEntry)
            ->setMaxResults($rowsPerPage)
            ->getQuery()->getResult();

        return array(
            'total' => $totalCount,
            'query' => $selectResult
        );
    }
}

包含概述的页面包括处理分页的树枝部分:

{#// Pageination#}
{% block body %}
    <div class="pagination" id="pagination" align="center">
        {% spaceless %}
            {% if lastPage > 1 %}

                {# the number of first and last pages to be displayed #}
                {% set extremePagesLimit = 3 %}

                {# the number of pages that are displayed around the active page #}
                {% set nearbyPagesLimit = 2 %}

                {#<pre>#}
                    {{ dump(currentFilters) }}
                    {#{{ dump(app.request.attributes) }}#}
                    {#{{ dump(app.request.attributes.get('_route_params')) }}#}
                {#</pre>#}

                <div class="pagination">
                    {% if currentPage > 1 %}
                        <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage-1})) }}">Previous</a>&nbsp;

                        {% for i in range(1, extremePagesLimit) if ( i < currentPage - nearbyPagesLimit ) %}
                            <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>&nbsp;
                        {% endfor %}

                        {% if extremePagesLimit + 1 < currentPage - nearbyPagesLimit %}
                            <span class="sep-dots">...</span>
                        {% endif %}

                        {% for i in range(currentPage-nearbyPagesLimit, currentPage-1) if ( i > 0 ) %}
                            <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>&nbsp;
                        {% endfor %}
                    {% elseif showAlwaysFirstAndLast %}
                        <span id="disabled">Previous</span>&nbsp;
                    {% endif %}

                    <a id="currentPage"
                       href="{{ path(paginationPath, currentFilters|merge({ page: currentPage })) }}"
                            >{{ currentPage }}</a>&nbsp;

                    {% if currentPage < lastPage %}
                        {% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %}
                            <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>&nbsp;
                        {% endfor %}

                        {% if  (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %}
                            <span class="sep-dots">...</span>
                        {% endif %}

                        {% for i in range(lastPage - extremePagesLimit+1, lastPage) if ( i > currentPage + nearbyPagesLimit ) %}
                            <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>&nbsp;
                        {% endfor %}

                        <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">Next</a>&nbsp;
                    {% elseif showAlwaysFirstAndLast %}
                        <span id="disabled">Next</span>
                    {% endif %}
                </div>
            {% endif %}
        {% endspaceless %}
    </div>
{% endblock %}

我对search-Array的序列化 - 解决方法感到不舒服。有没有更好的办法?还是更好的做法?

1 个答案:

答案 0 :(得分:0)

感谢你给我的提示 - 我也看了一下其他的包,最后找到了APYDataGridBundle,它解决了我所有的问题。在这里,我可以使用分页,搜索,我不必在twig中写表或在doctrine中编写db-querys。