通过它的PersistentCollection订购结果

时间:2016-07-04 10:52:49

标签: php mysql symfony orm dql

我有一个功能来获取所有" Tours By Network"今天。

我有一个这样的自定义存储库:

public function toursTodayByNetwork($network){

    $todayStart = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
    $todayEnd = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

    return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM AppBundle:Tour p JOIN AppBundle:Schedule s WHERE p.id = s.tour AND p.network = :network AND s.start >= :todayStart AND s.start <= :todayEnd'
        )
        ->setParameter('todayStart',$todayStart)
        ->setParameter('todayEnd',$todayEnd)
        ->setParameter('network',$network)
        ->getResult();

}

Tour实体与OneToMany实体的关系Object

我现在想要通过filiale中的AppBundle:Object列对整个内容进行排序,正如您所看到的那样,查询中没有,所以我不能只是ORDER BY。< / p>

我试过

/**
 * @ORM\OneToMany(targetEntity="Object", mappedBy="tour")
 * @ORM\OrderBy({"filiale" = "DESC"})
 */
protected $object;

但这并没有改变任何事情。

THIS看起来像我必须要做的事情,但我不知道在哪里放置EventListener?我也相信我可能只需要改变查询?

1 个答案:

答案 0 :(得分:0)

您必须加入对象表:

public function toursTodayByNetwork($network){

    $todayStart = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
    $todayEnd = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

    return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM AppBundle:Tour p JOIN p.schedule s JOIN p.object o WHERE p.network = :network AND s.start BETWEEN :todayStart AND :todayEnd ORDER BY o.filiale DESC'
        )
        ->setParameter('todayStart',$todayStart)
        ->setParameter('todayEnd',$todayEnd)
        ->setParameter('network',$network)
        ->getResult();

}

对于日期条件,BETWEEN是一种更好的做法,因为&gt; = AND&lt; =。