Symfony querybuilder orderby失败
在querybuilder中,我执行orderby,但如果我使用“leftjoin”进行orderby,我的orderby将不起作用,我不明白为什么 我的querybuilder:Symfony querybuilder orderby失败,symfony,query-builder,Symfony,Query Builder,在querybuilder中,我执行orderby,但如果我使用“leftjoin”进行orderby,我的orderby将不起作用,我不明白为什么 我的querybuilder: $qb = $this->createQueryBuilder('r') ->leftJoin('r.clientCategorisation', 'cc') ->leftJoin('cc.categorisation', '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;
如果我按“r”点餐,没关系,但如果我试着点“c”就不行了。。。
如果在我的视图html.twig中添加以下内容,我可以订购我的“c”asc:
{{ clientCategorisation.categorisation.id}}
我不明白为什么???此方法仅适用于“asc”,“desc”失败
编辑:我有一个datatable,在我的表中,我的顺序是在一个对象上:
A_resultats = $this->_O_resultatsR->getByRapportOrderByCat($idRapport, $A_onglet);
getByRapportOrderByCat是我的查询生成器
然后在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提供给模板(如果在AnalysisMyOrder中该对象的注释很好),我认为在模板中,我从querybuilder获得的订单被ORDERBY asc(基本ORDERBY)替换为order
对不起,我的英语不好
编辑2:
实体报告:
类结果
{
/**
*@var整数
*
*@ORM\Column(name=“id”,type=“integer”)
*@ORM\Id
*@ORM\GeneratedValue(strategy=“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;
}
}
实体客户分类:
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_results替换我的vieuw O_results中的O_尝试将字段添加为select参数并标记为,以便不修改水合流(对象而不是数组),如下所示:
$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;
希望此帮助在
分类
客户分类映射的关系中,您似乎有orderBy
标记或orderBy
注释。如果您有该注释,您的订单将通过在查询生成器
中创建的订单显式合并
例如:
<?php
/** @Entity **/
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group")
* @OrderBy({"id" = "ASC"})
**/
private $groups;
}
条令执行的最终查询将是:
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”})
请参阅。我的orderby不起作用请提供一条错误消息。我没有错误消息,但如果我有:1到9,在asc中我有1,2,3…9。这里我有2,3,5,8,1…你能写下你想在推文中使用的字段吗?但我有相同的问题:/那也不起作用,我的c.id不是orderby谢谢你的回答,但我有一个y orderby:/我已经编辑了我的第一篇文章,了解更多信息。您是否可以更新您的问题,包括您在查询中提到的3个模型和映射?
SELECT user FROM User LEFT JOIN user.groups group ORDER BY group.id ASC, group.name DESC;