symfony2查询构建器以复杂的方式

时间:2012-04-24 01:02:59

标签: mysql symfony doctrine query-builder

我需要知道这样做的最佳方式...我的数据库中有一个表格,对于这篇文章我们称之为TC,它只有我感兴趣的几个字段:

id, daydate, noteBy_id, and totaltime

noteBy_id被分配给用户表,该用户表再次只有我正在寻找的几个项目......

id, first_name, last_name, user_name, department, parentcompany

部门参考了第三个表格,其中只有iddepartmentName字段......

使用查询构建器,我到目前为止已经创建了这个:

$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] => me@me.me
                    [emailCanonical] => me@me.me
                    [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] => 
                )

        )

)

哪个错了,因为它只有一个项目在运行的查询中,它应该在数据库中有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个匹配项,那么结果数组中应该只有条目...对于查询此查询的任何帮助都表示赞赏..

1 个答案:

答案 0 :(得分: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(),
        ));
    }

这不是世界上最漂亮的功能,但它现在可以工作了,我会稍后修整它来清理一下......