排序不适用于Knp paginator

时间:2015-03-18 11:04:21

标签: php symfony sorting knppaginator

我正在使用knp paginator包,我收到此错误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 个答案:

答案 0 :(得分:3)

经过大量的拔毛,我终于找到了答案。 这是我的控制器

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>