【发布时间】:2017-05-17 11:34:55
【问题描述】:
我正在使用 querybuilder 将查询构建为 EntityRepository。我有一个 ManyToOne 关系(通过 ID),我试图通过关系的值对查询进行排序。代码如下:sn-ps。
namespace AppBundle\Entity\Repository;
class ContratoRepository extends EntityRepository
{
public function getWithoutParams($ops)
{
$query=$this->createQueryBuilder('e')
->orderBy('e.proc','ASC');
return $query->getQuery()->getResult();
}
现在是我的实体
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\ContratoRepository")
*/
class Contrato
{
...
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\ContratosAdmin\Proc")
* @ORM\JoinColumn(name="id_proc", referencedColumnName="id_proc")
* @ORM\OrderBy({"name" = "ASC"})
**/
private $proc;
现在我的问题是,是否可以通过我的 Proc 实体中的“名称”字段进行排序?就像现在一样,它是按 ID 排序的。
谢谢, 问候。
【问题讨论】:
-
尝试在
ContratoRepository中发出join请求以检索ContratoANDProc实体的数据。然后在您的 queryBuilder 请求中,您将拥有类似->orderBy('p.name','ASC');的内容,其中p代表您的Proc实体。 -
谢谢 Grechka 解决了我的问题。
标签: symfony doctrine-orm query-builder