0
我在教條2上遇到了一些麻煩。我想訂購一個查詢,但一週中的某一天。Doctrine2 select by orderby(case when)
這是什麼意思:
如果這裏給出的日期當天是Tuesday
,我想擁有它通過Tuesday, Wednseday, ..., Sunday, Monday
訂購。
但是表現可能有多天。
我來到這裏的代碼確實被
public function getValidPerformancesByDay2($date, $max = 25, $from, $asSql = false){
$myday = intVal(date('w',strtotime($date)));
$q = $this->getEntityManager()
->createQueryBuilder()
->select('DISTINCT textdesc,
CASE WHEN (perfdays.id < '. $myday .') THEN perfdays.id + 8
ELSE perfdays.id END AS HIDDEN sortvalue')
->from ('sys4winsegundaBundle:Performance','textdesc')
->join('textdesc.days', 'perfdays')
->where ('textdesc.enddate >= :date')
->andWhere('textdesc.isvalid = true')
->orderBy('sortvalue','ASC')
->setMaxResults($max)
->setFirstResult($from)
->setParameter('date',$date)
;
$query = $q->getQuery();
if ($asSql){
return $query;
}
return $query->getResult();
}
但該訂單的招不幸的是,當我看着已經發送它的查詢是:
SELECT DISTINCT p0_.id AS id0, p0_.name AS name1, p0_.duration AS duration2,
p0_.addedby AS addedby3, p0_.startdate AS startdate4, p0_.enddate AS enddate5,
p0_.starthour AS starthour6, p0_.flyer AS flyer7, p0_.price AS price8,
p0_.discount AS discount9, p0_.isvalid AS isvalid10,
p0_.archivedon AS archivedon11, p0_.description AS description12,
p0_.weblink AS weblink13, p0_.techinfo AS techinfo14, p0_.slug AS slug15,
CASE WHEN (d1_.id < 4) THEN d1_.id + 8 ELSE d1_.id END AS sclr16,
p0_.place_id AS place_id17, p0_.gallery_id AS gallery_id18 FROM performances
p0_ INNER JOIN performance_day p2_ ON p0_.id = p2_.performance_id
INNER JOIN days d1_ ON d1_.id = p2_.day_id WHERE p0_.enddate >= ? AND
p0_.isvalid = 1 ORDER BY sclr16 ASC OFFSET 0
Parameters: ['2012-08-23']
Time: 5.13 ms
這意味着如果表演一週發生3次,我發生3次。
有人有想法嗎?
編輯 我的英語是非常糟糕的,我lltry以不同的方式解釋: 嗯,我得到了在不同天發生文藝演出。
我想要做的是按時間順序排列它們。 但我發送到數據庫的方式是與一個開始日期,結束日期,然後它發生在幾天(星期二,星期三...)
我的查詢做到了這一點(按最近的一個排序),但作爲一些表現發生例如在星期三和星期五,我的查詢將返回該表演2次(星期三和星期五),而我應該只檢索每個表演的發生,但具有相同的順序(最近的第一個)
那麼我得到了不同日子發生的藝術表演。 –