Php 在Knp分页器中排序不工作

Php 在Knp分页器中排序不工作,php,symfony,sorting,knppaginator,Php,Symfony,Sorting,Knppaginator,我使用的是knp paginator包,我得到了这个错误给定的查询组件中没有这样的字段[catalogId],别名为[u]。如果我单击标题,排序工作正常,但如果我单击catalogid,则会显示错误。catalogid有多种关系。我在谷歌上搜索了答案,但似乎没有任何效果。你能告诉我如何修复此问题吗 这是我的控制器: public function indexAction(Request $request) { $em = $this->getDoctrine()->ge

我使用的是knp paginator包,我得到了这个错误
给定的查询组件中没有这样的字段[catalogId],别名为[u]
。如果我单击标题,排序工作正常,但如果我单击catalogid,则会显示错误。catalogid有多种关系。我在谷歌上搜索了答案,但似乎没有任何效果。你能告诉我如何修复此问题吗

这是我的控制器:

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>
<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_分页_排序(分页,'Title','u.Title')}
{{knp_分页_排序(分页,'SKU','u.catalogId')}
行动
{分页%中sku的%s}
{{loop.index}}
{{sku.title}
{{sku.catalogId.title}
{%endfor%}
{{knp_分页}

经过一番努力,我终于找到了答案。 这是我的控制器

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('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>
<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_分页_排序(分页,'Title','c.Title')}
{{knp_分页_排序(分页,'SKU','cl.catalogId.title')}
行动
{分页%中sku的%s}
{{loop.index}}
{{sku.title}
{{sku.catalogId.title}
{{dump(sku)}
{{}{{sku.catalogId.title}}{{
{%endfor%}
{{knp_分页}