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

标签 php symfony sorting knppaginator

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

这是我的 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,
    ]);
}

这是我的观点:

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

最佳答案

经过一番摸索,我终于找到了答案。 这是我的 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>

关于php - 排序在 Knp 分页器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29120352/

相关文章:

php - 订单日期查询,语法错误

php - 如何在 onFlush 事件监听器中更新实体的 manyToMany 集合?

python - Pandas 按条件按列值排名

c# - 排序列表 <DateTime> 降序

php - 如何实现不同权重的相同产品

php - 两个表之间的联接不返回表之间没有链接的行

symfony - Doctrine2 Entity PrePersist - 更新另一个实体

Symfony:如何根据 bundle 过滤已弃用的警报

c++ - 如何在数组的一次迭代中对 bool 数组进行排序(仅遍历数组一次)?

PHP:如何向后查找最后 N 行