0

これを行う最善の方法を知る必要があります...データベースにテーブルがあります。この投稿では、TCと呼びます。興味のあるフィールドはいくつかあります。

id, daydate, noteBy_id, and totaltime

noteBy_id は user テーブルに割り当てられていますが、ここにも探しているアイテムがいくつかあります...

id, first_name, last_name, user_name, department, parentcompany

id部門は、フィールドにandしかない 3 番目のテーブルを参照してい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] => 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つあるはずのアイテムが1つしかないため、これは間違っています..

次のようになります。

[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 つの一致がある場合、結果の配列にはエントリのみが存在するはずです...このクエリを理解するための助けをいただければ幸いです..

4

1 に答える 1

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(),
        ));
    }

これは世界で最も美しい関数ではありませんが、今のところはうまくいくでしょう。

于 2012-04-25T13:49:43.037 に答える