2010-05-22 2 views
4

데이터베이스에서 예정된 이벤트를 표시해야합니다. 문제는 현재 진행중인 시작일의 이벤트를 현재 사용하고있는 쿼리를 사용할 때 현재 이벤트에 관계없이 다가오는 이벤트 목록에 더 낮게 표시됩니다.SQL 시작일과 종료일로 예정된 이벤트를 선택하는 쿼리

내 테이블 (yaml) :

columns: 
    title: 
     type: string(255) 
     notnull: true 
     default: Untitled Event 
    start_time: 
     type: time 
    end_time: 
     type: time 
    start_day: 
     type: date 
     notnull: true 
    end_day: 
     type: date 
    description: 
     type: string(500) 
     default: This event has no description 
    category_id: integer 

내 쿼리 (교리) :

$results = Doctrine_Query::create() 
     ->from("sfEventItem e, e.Category c") 
     ->select("e.title, e.start_day, e.description, e.category_id, e.slug") 
     ->addSelect("c.title, c.slug") 
     ->orderBy("e.start_day, e.start_time, e.title") 
     ->limit(5) 
     ->execute(array(), Doctrine_Core::HYDRATE_ARRAY); 

은 기본적으로 저는 현재 진행되고있는 이벤트를하고 싶은 상단에로 (그렇다면 오늘 START_DAY와 END_DAY 사이입니다) 명부. 가능하다면 어떻게해야합니까? Raw SQL 쿼리는 DQL로 전환하기가 쉽기 때문에 좋은 대답이기도합니다. 아래 답변 중 하나를 기반으로

솔루션 는 :

$results = Doctrine_Query::create() 
     ->from("sfEventItem e, e.Category c") 
     ->select("e.title, e.start_day, e.description, e.category_id, e.slug") 
     ->addSelect("c.title, c.slug") 
     ->addSelect("IF(NOW() BETWEEN e.start_day AND e.end_day,0,1) as score") 
     ->orderBy("score, e.start_day, e.start_time, e.title") 
     ->limit(5) 
     ->execute(array(), Doctrine_Core::HYDRATE_ARRAY); 

답변

3

이 수행해야합니다 당신은 case을 사용할 수 있습니다

->orderBy("IF(NOW() BETWEEN e.start_day AND e.end_day,0,1) , e.start_day, e.start_time, e.title") 
+0

그건 꽤 깔끔하고 약간의 수정으로 작동했습니다. (어떤 이유로 doctrine은 orderBy 일 때 어떤 레코드라도 선택하지 않았습니다) –

2

. 현재 날짜는 시작과 끝 날짜 사이 일 때

는 점수로 점수 0

그리고 주문 점수, 1 모든 다른 경우를 추가합니다.

+0

감사합니다. 불행히도 Doctrine은 (우리가 편의를 위해 지불하는 가격)을 지원하지 않습니다. –