简体   繁体   中英

How to find sql queries to get weeks between dates in doctrine

I am writing this repository function to get the values of all week..and its working good. Now I need find the result between two dates. I have tried something but its not working

public function getActivitiesByWeek($patient_id) {        
    $fromDate = new \DateTime('2014-05-07');
    $fromDate = (array)$fromDate;
    $toDate = new \DateTime('2014-05-25');
    $toDate = (array)$toDate;
    $rsm = new ResultSetMapping();
    $rsm->addEntityResult('Application\Model\PatientActivity', 'p');
    $rsm->addScalarResult('weeks', 'weeks');
    $rsm->addScalarResult('maxActivityDate', 'maxActivityDate');
    $rsm->addScalarResult('counts', 'counts');
    $query = $this->_em->createNativeQuery("SELECT
        YEARWEEK(activity_date) AS weeks,
        MAX(activity_date) maxActivityDate,
        SUM(activity_count) AS counts
        FROM patient_activity
        WHERE patient_id = $patient_id
        GROUP BY weeks
        ORDER BY activity_date ASC;", $rsm);
    $activities = $query->getArrayResult();
    //echo "<pre>";print_r($activities);die;
    return $activities;
}

How can I find the weeks and values between the two dates?

To create a query that searches between two dates, use something like the format below:

 $qb->select('p')
     ->where('p.activity_date' BETWEEN :fromDate AND :toDate')
     ->setParameter('fromDate', $fromDate->format('Y-m-d'))
     ->setParameter('toDate', $toDate->format('Y-m-d'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM