2017-02-13 140 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