Symfony2 + Doctrine + case语句:如何在DQL或其他语句中做一个case语句?

时间:2012-01-23 15:24:59

标签: symfony doctrine-orm

我必须在我的查询构建器中使用 case statements ...

我的查询是这样的:

        $query = $this->getEntityManager('V')->createQuery(
            'SELECT  V,
                (CASE unit  WHEN "DAY"      THEN DATE_ADD( NOW() , INTERVAL start_date DAY ) 
                            WHEN "MONTH"    THEN DATE_ADD( NOW() , INTERVAL start_date MONTH )
                            WHEN "YEAR"     THEN DATE_ADD( NOW() , INTERVAL start_date YEAR ) 
                 END as startDate
                ) 
            FROM AppMyBundle:Entity V

             WHERE 1=1 
             ORDER BY date ASC'
        );

请注意,它在MySQL中是正确的。

我不知道如何解决它......

如果您有其他想法,请随时帮助我:)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

$query =  $this->getDoctrine()->getEntityManager()->getConnection()->prepare(
        "SELECT  V.id,
            (CASE WHEN "DAY"      THEN DATE_ADD( NOW() , INTERVAL start_date DAY ) 
                  WHEN "MONTH"    THEN DATE_ADD( NOW() , INTERVAL start_date MONTH )
                  WHEN "YEAR"     THEN DATE_ADD( NOW() , INTERVAL start_date YEAR ) 
             END as startDate
            ) 
        FROM table_name V

         WHERE V.id = :id 
         ORDER BY date ASC"
    );
 $query->bindValue('id', $id);
 $query->execute();   
 $results = $query->fetchAll(); 

试试这个我希望它适合你 谢谢, Ashok Chitroda