Sorting not working in Knp paginator

此生再无相见时 提交于 2019-12-10 17:38:03

问题


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??

this is my controller:

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,
    ]);
}

this is my view:

<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:


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,
        ]);
    }

this is my repository:

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();
    }

this is my view:

<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>


来源:https://stackoverflow.com/questions/29120352/sorting-not-working-in-knp-paginator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!