2017-02-13 133 views
0

在查詢生成器我做了一個orderby,但如果我orderby'leftjoin'我orderby不起作用,我不明白爲什麼。Symfony查詢生成器orderby失敗

我的QueryBuilder:

 $qb = $this->createQueryBuilder('r') 
     ->leftJoin('r.clientCategorisation', 'cc') 
     ->leftJoin('cc.categorisation', 'c') 
     ->addOrderBy('c.id','ASC') 
    $a_result = $qb->getQuery()->getResult(); 
    return $a_result; 

如果我排序依據我的「R」,其確定,但如果我嘗試命令「C」是簡化版,工作...... 我可以爲了我的「C」 asc如果在我看來html.twig我加:

{{ clientCategorisation.categorisation.id}} 

我不明白爲什麼?而這種方法只適用於'asc','desc'失敗。

編輯:我有一個DataTable,並在我的表我的訂單是在對象上:

A_resultats = $this->_O_resultatsR->getByRapportOrderByCat($idRapport, $A_onglet); 

getByRapportOrderByCat是我的QueryBuilder。

然後在foreach:

foreach ($A_resultats as $I_key => $O_resultat) { 

我建我的專欄,並在此列 '分析':

$A_valuesForTableau[$I_key]['analyse'] = $this->render('mypath', array(
        'O_resultat'  => $O_resultat, 
        'A_clientCats'  => $A_clientCats, 
      ))->getContent(); 

我給再我的目標$ O_resultat到模板(如果評論這個對象在分析我的訂單是很好的),我認爲在模板中,我的來自querybuilder的訂單被asc(基本訂單by)替代。

對不起,我的英語不好。

EDIT2: 實體resutat: 類Resultat { /** * @var整數 * * @ORM \柱(名稱= 「ID」,類型= 「整數」) * @ORM \ Id * @ORM \ GeneratedValue(strategy =「AUTO」) */ private $ id;

/** 
    * @ORM\ManyToOne(targetEntity="ClientCategorisation") 
    * @ORM\JoinColumn(name="client_categorisation", referencedColumnName="id", onDelete="CASCADE", nullable=true) 
    */ 
    private $clientCategorisation; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set clientCategorisation 
    * 
    * @param ClientCategorisation $clientCategorisation 
    * @return Resultat 
    */ 
    public function setClientCategorisation(ClientCategorisation $clientCategorisation = null) 
    { 
     $this->clientCategorisation = $clientCategorisation; 

     return $this; 
    } 

    /** 
    * Get clientCategorisation 
    * 
    * @return ClientCategorisation 
    */ 
    public function getClientCategorisation() 
    { 
     return $this->clientCategorisation; 
    } 

} 

實體clientcategorisation:

class ClientCategorisation 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Categorisation") 
    * @ORM\JoinColumn(name="categorisation", referencedColumnName="id", onDelete="CASCADE") 
    */ 
    private $categorisation; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set categorisation 
    * 
    * @param string $categorisation 
    * @return ClientCategorisation 
    */ 
    public function setCategorisation($categorisation) 
    { 
     $this->categorisation = $categorisation; 

     return $this; 
    } 

    /** 
    * Get categorisation 
    * 
    * @return string 
    */ 
    public function getCategorisation() 
    { 
     return $this->categorisation; 
    } 

} 

分類實體:

class Categorisation 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nom", type="string", length=255) 
    */ 
    private $nom; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ordre", type="integer", nullable=true) 
    */ 
    private $ordre; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set nom 
    * 
    * @param string $nom 
    * @return Categorisation 
    */ 
    public function setNom($nom) 
    { 
     $this->nom = $nom; 

     return $this; 
    } 

    /** 
    * Get nom 
    * 
    * @return string 
    */ 
    public function getNom() 
    { 
     return $this->nom; 
    } 


    /** 
    * Set ordre 
    * 
    * @param integer $ordre 
    * @return Categorisation 
    */ 
    public function setOrdre($ordre) 
    { 
     $this->ordre = $ordre; 

     return $this; 
    } 

    /** 
    * Get ordre 
    * 
    * @return integer 
    */ 
    public function getOrdre() 
    { 
     return $this->ordre; 
    } 
} 

EDIT2:我已經找到了解決辦法,當我在我的模板中使用 'O_resultat' 我的訂單失敗,所以我得到'A_resultats',那樣的工作:

{% for O_resultats in A_resultats %} 
    {% if O_resultats.id == O_resultat.id %} 

和我在vieuw O_resultat替代由O_resultats

+1

*我的訂單不起作用*請提供錯誤消息。 –

+0

我沒有錯誤信息,但如果我有:1至9,在ASC我有1,2,3 ... 9。 在這裏我有2,3,5,8,1 ... – Ygg69

+0

你可以寫你想在樹枝中使用的字段嗎 –

回答

0

嘗試以不修改水化流(對象,而不是陣列)如下添加字段作爲選擇參數和標記爲hidden

$qb = $this->createQueryBuilder('r') 
     ->addSelect('c.id as HIDDEN mysort') 
     ->leftJoin('r.clientCategorisation', 'cc') 
     ->leftJoin('cc.categorisation', 'c') 
     ->addOrderBy('mysort','ASC'); 
    $a_result = $qb->getQuery()->getResult(); 

    return $a_result; 

希望這有助於

+0

Thnaks但我有同樣的問題:/那也行不了,我的c .id不是訂單 – Ygg69

0

好像你有order-by標籤或orderBy標註在clientCategorisation映射categorisation關係。如果您有該註釋,則您的訂單將通過在query builder中創建的訂單明確合併。

例子:

<?php 

/** @Entity **/ 
class User 
{ 
    // ... 

    /** 
    * @ManyToMany(targetEntity="Group") 
    * @OrderBy({"id" = "ASC"}) 
    **/ 
    private $groups; 
} 

如果你創建這樣的查詢:

$qb = $this->createQueryBuilder('user') 
    ->leftJoin('user.groups', 'group') 
    ->orderBy('group.name', 'DESC') 
; 

由學說執行最後的查詢將是:

SELECT user FROM User LEFT JOIN user.groups group ORDER BY group.id ASC, group.name DESC; 

這樣的結果將被責令通過group.id ASC(在您的映射中定義),然後通過group.name DESC(在查詢生成器中定義)。

如果您希望能夠按group.name DESC排序(在示例中),您必須在註釋中刪除@OrderBy({"id" = "ASC"})

請參閱Ordering To-Many Associations

+0

感謝您的回答,但我有任何orderby:/ 我已編輯我的第一篇文章以獲取更多信息 – Ygg69

+0

您是否可以更新您的問題,包括您在查詢中提到的3種模型和映射? –

+0

我已更新它 – Ygg69