排序在 Knp 分页器中不起作用

Posted

技术标签:

【中文标题】排序在 Knp 分页器中不起作用【英文标题】:Sorting not working in Knp paginator 【发布时间】:2015-03-18 11:04:20 【问题描述】:

我正在使用 knp 分页器包,我收到此错误 There is no such field [catalogId] in the given Query component, aliased by [u]。如果我单击标题,排序工作正常,但如果我单击 catalogid,它会显示一个错误。CatalogId 是 ManytoOne 关系。我已经用谷歌搜索了答案,但似乎没有什么对我有用。你能告诉我如何解决这个问题吗? ?

这是我的控制器:

public function indexAction(Request $request)
   
    $em = $this->getDoctrine()->getManager();
    $postData = $request->query->all();

    $form = $this->createForm(new SkuInventoryType(), new SkuInventory());
    $form->handleRequest($request); 
    if($postData) 
        $repo = $this->getDoctrine()->getRepository('RetailMappingCatalogBundle:SkuInventory');
        //dump($repo);die;
        $skuQuery = $repo->createQueryBuilder('u')
                        ->orderBy($postData['sort'],$postData['direction']);
    else
        $skuQuery = $em->getRepository('RetailMappingCatalogBundle:SkuInventory:u')->findAll();
    
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
                $skuQuery, 
                $request->query->get('page', 1), 
                15
            );
    if ($form->isSubmitted() && $form->isValid())
       
        $data = $form->getData(); 
        $data->setCreatedBy($this->getUser());
        $data->setUpdatedBy($this->getUser());

        $em->persist($data);
        $em->flush();
        $alertMessage = $this->get('retail_mapping.alert_message');
        $alertMessage->success('SKU Inventory Created');

        return $this->redirect($this->generateUrl('sku_inventory'));
    

    return $this->render('User/SkuInventory/index.html.twig',[
            'form' => $form->createView(), 
            'pagination' => $pagination,
    ]);

这是我的看法:

<table class="table table-bordered table-striped">
            <tr>
                <th>Id</th>

               <th% if pagination.isSorted('u.title') % class="sorted"% endif %> knp_pagination_sortable(pagination, 'Title', 'u.title') </th>
               <th% if pagination.isSorted('u.catalogId') % class="sorted"% endif %> knp_pagination_sortable(pagination, 'SKU', 'u.catalogId') </th>
               <th>Actions</th>
            </tr>

        % for sku in pagination %

            <tr>
                <td> loop.index </td>
                <td> sku.title</td>
                <td> sku.catalogId.title</td>
                <td><a class="btn btn-primary" href="path('sku_inventory_edit','id': sku.id)">Edit</a>&nbsp;&nbsp;
                <a class="btn btn-primary" href="path('sku_inventory_delete','id': sku.id)">Delete</a></td>
            </tr>
            % endfor %

            </table>

            <div class="navigation">
                 knp_pagination_render(pagination) 
            </div>

【问题讨论】:

【参考方案1】:

在拉了很多头发之后,我终于找到了答案。 这是我的控制器

public function indexAction(Request $request)
       
        $breadcrumbs = $this->get("white_october_breadcrumbs");
        $breadcrumbs->addItem("Sku Inventory", $this->get("router")->generate("index"));
        $em = $this->getDoctrine()->getManager();

        $form = $this->createForm(new SkuInventoryType(), new SkuInventory());
        $form->handleRequest($request); 

        $repo = $em->getRepository('RetailMappingCatalogBundle:SkuInventory')->findAllCatalog();
        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                        $repo, 
                        $request->query->get('page', 1), 
                        15
                    );
        if ($form->isSubmitted() && $form->isValid())
           
            $data = $form->getData(); 
            $data->setCreatedBy($this->getUser());
            $data->setUpdatedBy($this->getUser());

            $em->persist($data);
            $em->flush();
            $alertMessage = $this->get('retail_mapping.alert_message');
            $alertMessage->success('SKU Inventory Created');

            return $this->redirect($this->generateUrl('sku_inventory'));
        

        return $this->render('User/SkuInventory/index.html.twig',[
                'form' => $form->createView(), 
                'pagination' => $pagination,
        ]);
    

这是我的仓库:

public function findAllCatalog()
    
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder();
        $qb->select('c')
        ->from('RetailMappingCatalogBundle:SkuInventory', 'c')
        ->Join('c.catalogId', 'cl')
        ->orderBy('c.id', 'DESC');
        //dump($qb->getQuery());die;
        return $qb->getQuery();
    

这是我的看法:

<table class="table table-bordered table-striped">
            <tr>
                <th>Id</th>

               <th% if pagination.isSorted('c.title') % class="sorted"% endif %> knp_pagination_sortable(pagination, 'Title', 'c.title') </th>
               <th% if pagination.isSorted('cl.catalogId.title') % class="sorted"% endif %> knp_pagination_sortable(pagination, 'SKU', 'cl.catalogId.title') </th>
               <th>Actions</th>
            </tr>

        % for sku in pagination %

            <tr>
                <td> loop.index </td>
                <td> sku.title</td>
                <td> sku.catalogId.title</td>
                 dump(sku) 
                #<td> sku.catalogId.title</td>#
                <td><a class="btn btn-primary" href="path('sku_inventory_edit','id': sku.id)">Edit</a>&nbsp;&nbsp;
                <a class="btn btn-primary" href="path('sku_inventory_delete','id': sku.id)">Delete</a></td>
            </tr>
            % endfor %

            </table>

            <div class="navigation">
                 knp_pagination_render(pagination) 
            </div>

【讨论】:

以上是关于排序在 Knp 分页器中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

直接使用分页器并在关联列上定义排序不起作用

如何降级 symfony2 捆绑包 knp_compnent?

KNP 分页链接卡在第 1 页

如何在 Twig 中为 KNP 分页器获取分页方向?

使用 ajax 时,Tablesorter 禁用分页器按钮不起作用

Knp分页器不分页