2014-06-17 65 views
0

我有我的實體條目,並希望返回最近15天的日期和條目數的計數,並將數據顯示在圖表中。創建原則查詢

但是,DQL正在寫作是某種失敗,不會帶來我想要的結果,如果有人可能會提供幫助,我將不勝感激。

這是我想在MySQL

SELECT count(*) as total, DATE_FORMAT(e.created_at, '%Y-%m-%d') as date FROM entries e WHERE e.created_at BETWEEN DATE_SUB(CURDATE(),INTERVAL 15 DAY) AND CURDATE() GROUP BY date 

這裏實現查詢我有:

$sql = "SELECT count(*) as total, DATE_FORMAT(e.created_at, '%Y-%m-%d') as date FROM entries e WHERE e.created_at BETWEEN DATE_SUB(CURDATE(),INTERVAL 15 DAY) AND CURDATE() GROUP BY date"; 
    $q = $this->em->createQuery($sql); 
    $registered = $query->getResult(); 

回答

1

試試這個:

$ago = new \DateTime('15 days ago'); 
$now = new \DateTime('now'); 

$qb = $this->em->createQueryBuilder(); 
$qb->select('e.created_at'); 
$qb->from('entries', 'e'); 
$qb->where('e.created_at BETWEEN :ago AND :now') 
$qb->setParameter('ago', $ago->format('Y-m-d')) 
$qb->setParameter('now', $now->format('Y-m-d')); 

// get results 
$dates = $qb->getQuery()->getArrayResult(); 

注意format()方法應該匹配您的列日期格式

$dates是一個包含匹配日期的數組。如果你想讓你知道計數,那麼你可以這樣做:

$count = count($dates);