2014-01-27 47 views
0

我使用的是KNP分頁程序包,一切正常,直到我嘗試使用一對多關係關聯進行字段排序。我在purchaseOrder表中有一個名爲「supplier_id」的文件,我試圖對此進行排序,但我一直在收到「沒有這樣的字段...」錯誤。 「supplier_id」字段僅包含「供應商」表中的ID號。KNP分頁程序包查詢問題

這是我的代碼。

 // 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 
     ); 

所以基本上在頁面第一次加載它搶佔了所有采購訂單。

我然後建立這個能夠通過供應商進行排序:

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

當我點擊它來排序它給了我這個錯誤:

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

我的問題是我怎麼樣當我排序的領域是一對多關係的一部分。該ID存儲在名爲「supplier_id」的字段中,但它是OtoM關係。

謝謝!

回答

0

長話短說,你需要做一個解決。

你基本上需要有一個正常的查詢獲取所有對象的ID以你想要的方式排序。通過這些ID,您可以構建一個新查詢,您只需查找這些ID並對該查詢進行分頁即可。

0

你可以試試用QueryBuilder Doctrine創建的DQL的paginator。