2012-04-24 50 views
0

我需要知道最好的方法去做這件事......我在我的數據庫中有一個表格,對於這篇文章我們將它稱爲TC,它只有幾個字段I感興趣的是:symfony2以複雜的方式查詢生成器

id, daydate, noteBy_id, and totaltime

noteBy_id分配給用戶表,其中有一次只有幾個項目我要找...

id, first_name, last_name, user_name, department, parentcompany

部門參考尚第三個表格只有id在田裏departmentName ...

使用查詢生成器,我已經創造了這個至今:

$query = $qb->select('t', 'u') 
->leftJoin('t.noteBy', 'u', 'WITH', 'u.id = 't.noteBy') 
->where('t.daydate BETWEEN :start AND :end') 
->andWhere('u.parentcompany = :pid') 
->andWhere('u.department = :dep) 
->groupBy('u.id') 
->setParameter('start', $start) 
->setParameter('end', $end) 
->setParameter('dep', $department) 
->getQuery(); 

現在,這種查詢可以作爲信息,但它返回:

Array 
(
    [0] => Array 
     (
      [id] => 6 
      [in1] => DateTime Object 
       (
        [date] => 2012-04-17 09:10:00 
        [timezone_type] => 3 
        [timezone] => America/Denver 
       ) 

      [out1] => 
      [in2] => 
      [out2] => 
      [in3] => 
      [out3] => 
      [totaltime] => 0.00 
      [daydate] => DateTime Object 
       (
        [date] => 2012-04-17 09:10:00 
        [timezone_type] => 3 
        [timezone] => America/Denver 
       ) 

      [noteBy] => Array 
       (
        [username] => j 
        [usernameCanonical] => j 
        [email] => [email protected] 
        [emailCanonical] => [email protected] 
        [enabled] => 1 
        [salt] => 
        [password] =>  
        [credentialsExpired] => 
        [credentialsExpireAt] => 
        [id] => 124 
        [full_name] => 
        [first_name] => J 
        [last_name] => B 
        [socialsecurity] => 
        [phone] => 
        [phone2] => 
        [address1] => 
        [address2] => 
        [city] => 
        [state] => 
        [zip] => 
        [startdate] => 
        [notes] => 
       ) 

     ) 

) 

這是錯誤的,因爲它只有1個項目,在運行的查詢中應該有2個數據庫..

它應該是什麼樣子的:

[0] 
    (
    [id] => '1' 
    [first_name] => 'j' 
    [last_name] => 'b' 
    [username] => 'jb' 
    [department] => 'some department' 
    [total_time] => SUM of all records in TC.total_time for user id 1 
) 
[1] 
    (
    [id] => '2' 
    [first_name] => 'frank' 
    [last_name] => 'blew' 
    [username] => 'fb' 
    [department] => 'some department' 
    [total_time] => SUM of all records in TC.total_time for user id 2 
) 

但是,應該只有結果數組中的條目,如果用戶ID已在TC表中至少1場......在盤算這個查詢出來表示讚賞任何幫助..

+0

什麼是您的第三剪輯$ qb?如果你使用''qb = $ this-> createQueryBuilder('t');''你可能想嘗試使用'$$ qb = $ this - > _ em-> createQueryBuilder();''一個乾淨的queryBuilder,其中的createQueryBuilder註冊您的選擇使得使用自己的選擇困難。 – Sgoettschkes 2012-04-24 21:58:31

回答

0

我結束了開溝的QueryBuilder並用常規的查詢會...

的函數現在看起來是這樣的:

public function indexAction($start = null, $end = null) { 
     $request = $this->get('request'); 
     /** 
     * The following 4 lines, are to default the date to Sunday through Saturday of PREVIOUS WEEK 
     */ 
     $department = null; 
     $em = $this->getDoctrine()->getEntityManager(); 
     $user = $this->get('security.context')->getToken()->getUser(); 

     $dateRangeForm = $this->createForm(new TimeClockReportType()); 
     if ($request->getMethod() == "POST") { 
      $dateRangeForm->bindRequest($request); 
      $formData1 = $dateRangeForm->getData(); 

     } 
     if ($request->getMethod() == "POST" && $formData1['salesCompany']->getDepartmentName() != NULL) { 
      $formData = $dateRangeForm->getData(); 
      $start = $formData['dateRange']['startDate']; 
      $end = $formData['dateRange']['endDate']; 
      $department = $formData['salesCompany']->getDepartmentName(); 
      $query = "SELECT SUM(t.totaltime) AS total_time, u.id, u.first_name, u.last_name, u.username, d.departmentName 
        FROM EcsCrmBundle:TimeClock t 
        LEFT JOIN t.noteBy AS u 
        LEFT JOIN u.employeeDepartment AS d 
        WHERE t.noteBy = u.id 
        AND u.employeeDepartment = :dep 
        AND t.daydate BETWEEN :start AND :end 
        AND u.parentcompany = :pid 
        HAVING total_time > 0"; 
      $query = $em->createQuery($query) 
       ->setParameter('start', $start) 
       ->setParameter('end', $end) 
       ->setParameter('pid', $user->getParentcompany()) 
       ->setParameter('dep', $department); 
     } else { 
      if ($request->getMethod() == "POST") { 
       $formData = $dateRangeForm->getData(); 
       $start = $formData['dateRange']['startDate']; 
       $end = $formData['dateRange']['endDate']; 
      } else { 
       $today = strtotime('-7 day', time()); 
       $end = date("Y-m-d 23:59:59", strtotime('next saturday', $today)); 
       $start = date("Y-m-d 00:00:00", strtotime('last sunday', $today)); 
      } 


      $query = "SELECT SUM(t.totaltime) AS total_time, u.id, u.first_name, u.last_name, u.username, d.departmentName 
        FROM EcsCrmBundle:TimeClock t 
        LEFT JOIN t.noteBy AS u 
        LEFT JOIN u.employeeDepartment AS d 
        WHERE t.noteBy = u.id 
        AND t.daydate BETWEEN :start AND :end 
        AND u.parentcompany = :pid 
        GROUP BY u.id 
        HAVING total_time > 0"; 
      $query = $em->createQuery($query) 
         ->setParameter('start', $start) 
         ->setParameter('end', $end) 
         ->setParameter('pid', $user->getParentcompany()); 

     } 
     $entity = $query->getResult(); 
     return $this->render('EcsCrmBundle:Reports:TimeClockReport.html.twig', array(
      'entity' => $entity, 
      'start' => $start, 
      'end' => $end, 
      'dep' => $department, 
      'form' => $dateRangeForm->createView(), 
     )); 
    } 

這不是世界上最漂亮的功能,但它會爲現在的工作和我會化妝它後來清理了一下...