0

I am using the KNP Paginator bundle and everything works fine until I try to sort on field with a one to many relationship association. I have a filed in the purchaseOrder table called "supplier_id" and I am trying to sort on that, but I keep getting a "There is no such field..." error. The "supplier_id" field just holds an ID number from "Supplier" table.

Here is my code.

        // Set the up the pagination statement...
        $em = $this->getDoctrine()->getManager();
        $dql   = "SELECT p FROM WIC\PurchaseOrderBundle\Entity\PurchaseOrder p WHERE p.account=:account_id ORDER BY p.id desc";
        $query = $em->createQuery($dql);
        $query->setParameters(array(
            'account_id' => $account->getId(),
        ));;

        $paginator  = $this->get('knp_paginator');
        $paginatorObject = $paginator->paginate(
            $query,
            $this->get('request')->query->get('page', 1),25
        );

So basically on the first load of the page it is grabbing all purchase orders.

I then set up this to be able to sort by supplier:

 <th>{{ knp_pagination_sortable(purchaseOrders, 'Supplier', 'p.supplier') }}</th>

When I click it to sort it gives me this error:

 There is no such field [supplier_id] in the given Query component, aliased by [p]

My question is how do I sort when the field I am sorting on is part of a one to many relationship. The ID is stored in a field called "supplier_id" but it is a OtoM relationship.

Thanks!

4

2 回答 2

0

您可以尝试使用使用 QueryBuilder Doctrine 创建的 DQL 的分页器。

于 2014-07-18T12:12:54.987 回答
0

长话短说,你需要做一个工作。

你基本上需要有一个正常的查询来获取所有按你想要的方式排序的对象的 id。然后使用这些 id 构建一个新查询,您只需在其中查找这些 id 并对该查询进行分页。

于 2014-01-28T12:05:42.550 回答