2017-02-13 8 views
0

質問ビルダでは、私はorderbyを行いますが、もし私が 'leftjoin'していれば、私のorderbyは動作せず、なぜ私は理解できません。symfony querybuilder orderby failed

マイquerybuilder:

私は私の 'R'、その[OK]をORDERBYが、私は仕事をdoes't 'C' ... を注文しようとした場合、私は私の 'C' を注文することができた場合
 $qb = $this->createQueryBuilder('r') 
     ->leftJoin('r.clientCategorisation', 'cc') 
     ->leftJoin('cc.categorisation', 'c') 
     ->addOrderBy('c.id','ASC') 
    $a_result = $qb->getQuery()->getResult(); 
    return $a_result; 

私の見解であれば昇順html.twig私は追加:

{{ clientCategorisation.categorisation.id}} 

私はなぜ理解できないのですか?そして、このメソッドは 'asc'のためにのみ機能し、 'desc'は失敗しました。

編集:私はデータテーブルを持っているし、私のテーブルに注文が対象である:

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

getByRapportOrderByCatは私querybuilderです。

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

私は私の列を構築し、この欄に '分析':次にforeachの中

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

私はコメントがあれば(テンプレートに再び私のオブジェクト$ O_resultatを与えます私の注文を分析するにはこのオブジェクトがいいです)と私はテンプレートで私の注文は、ascii(基本的な順序で)順序で置き換えるquerybuilderからだと思う。

私の悪い英語のために申し訳ありません。

EDIT2: エンティティresutat: クラスResultat {/ ** * @var整数 * * @ORMの\カラム(名= "ID"、タイプ= "整数") * @ORM \ Id * @ORM \ GeneratedValue(戦略= "AUTO") */ プライベート$ 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 %} 

と私はO_resultats

+1

*私のorderbyは動作しません。*エラーメッセージを提供してください。 –

+0

私はエラーメッセージがありませんが、もし私が持っている:1から9まで、私は1,2,3 ... 9を持っています。 ここで私は2,3,5,8,1 ... – Ygg69

+0

あなたはあなたの枝に使用したいフィールドを書くことができます –

答えて

0

によって私vieuw O_resultatに次のように水分補給フロー(配列の代わりにオブジェクト)を変更しないために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タグやclientCategorisationマッピングのcategorisation関係でorderBy注釈を持っているように思えます。その注釈がある場合、注文は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(マッピングで定義されています)、で注文DESC(クエリビルダで定義されています)。

group.name DESC(例)で並べ替えることができる場合は、注釈の@OrderBy({"id" = "ASC"})を削除する必要があります。

Ordering To-Many Associationsを参照してください。

+0

あなたの答えはありがたいですが、私はorderbyを持っています:/ 詳細については私の最初の投稿を編集しています – Ygg69

+0

3モデルとクエリで言及したマッピングを含むあなたの質問を更新できますか? –

+0

私はそれを更新しました – Ygg69

関連する問題