本文介绍了排序在Knp分页器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knp分页器包,但出现此错误There is no such field [catalogId] in the given Query component, aliased by [u].如果单击标题,排序效果很好,但是如果单击catalogid,则显示错误.CatalogId具有ManytoOne关系.我已经在Google上搜索了答案,但似乎对我没有用.你能告诉我如何解决这个问题吗? ?

I am using the knp paginator bundle and i got this error There is no such field [catalogId] in the given Query component, aliased by [u] . Sorting works fine if i click in the title but it shows me an error if i click in the catalogid.CatalogId is of ManytoOne relationship.I have googled for answers but nothing seems to work for me.Can u tell me how to fix this??

这是我的控制器:

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>

推荐答案

经过一番梳理,我终于找到了答案.这是我的控制器

After a lot of hair pulling I finally found the answer.This is my controller

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分页器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:41