Symfony2 + Doctrine + case statements: how to do a

2019-04-16 01:22发布

I have to use case statements into my query builder...

My query is something like this:

        $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'
        );

Note that it is correct in MySQL.

I don't know how to solve it...

If you have another idea, feel free to help me :)

Any idea?

1条回答
做自己的国王
2楼-- · 2019-04-16 02:24
$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(); 

Try this one I hope it working for you Thanks, Ashok Chitroda

查看更多
登录 后发表回答