2016-08-11 45 views
1

我通过堆栈日期时间试图组我的实体。每个实体都有以下各列:ID,设备,启动,停止,ip地址。(Symfony的3.1)PHP主义集团机构通过日期时间

我希望能够得到一个范围的日期,并将它们按日期(天)范围内的实体与每一天有多少小时有以下。

这是我迄今为止

public function findAllGroupedByDate($from = null, $to = null) 
{ 
    $from = isset($from) && !is_null($from) ? $from : date('Y-m-d', time()); 
    $to = isset($to) && !is_null($to) ? $to : date('Y-m-d', time()); 

    $from = new \DateTime("-3 days"); 
    $to = new \DateTime("1 days"); 

    $from = date("Y-m-d", $from->getTimestamp()); 
    $to = date("Y-m-d", $to->getTimestamp()); 

    $q = $this->getEntityManager()->createQueryBuilder() 
     ->select("timelog") 
     ->from("AppBundle:Timelog", "timelog") 
     ->where("timelog.start BETWEEN :from AND :to") 
     ->setParameter("from", $from) 
     ->setParameter("to", $to) 
     ->orderBy("timelog.stop") 
     ->getQuery(); 
    return $q->getResult(); 
} 

public function findFromToday() 
{ 
    $q = $this->createQueryBuilder('t') 
      ->select('t.id', 't.start', 't.stop', 't.stop') 
      ->where("t.start >= :today") 
      ->setParameter("today", date('Y-m-d', time())) 
      ->groupBy("t.id", "t.stop") 
      ->orderBy("t.start", "asc") 
      ->getQuery(); 

    return $q->getResult(); 
} 

这是我的仓库类的代码。

,并从我的控制器看起来像这样的代码:

$timelogsRepo = $this->getDoctrine()->getRepository('AppBundle:Timelog'); 

// Grab logs from today 
$timelogsCollection = $timelogsRepo->findFromToday(); 
$tmpLogs = $timelogsRepo->findAllGroupedByDate(); 

// THIS SECTION IS FOR CALCULATING HOURS & MINUTES 
    $minutes = 0; 
    $hours = 0; 
    foreach($timelogsCollection as $log) 
    { 
     $time1 = $log['start']; 
     $time2 = $log['stop']; 
     $interval = date_diff($time1, $time2); 

     $hours += $interval->h; 
     $minutes += $interval->i; 

    } 


    $minutes = $minutes >59 ? $minutes/60 : $minutes; 


    return $this->render(':Timelog:index.html.twig', [ 
     'timelogs' => $logsOut, 
     'hours' => $hours, 
     'minutes' => $minutes 
    ]); 

到目前为止我能计算总花费的时间对于一个给定的一天(只有一天)。现在我想获得全部实体,由同日(日)他们组和间隔返回数据。

示例数据库表看起来像这样[ID,DEVICE_ID,启动,停止,ip地址]

1 1 2016-08-09 09:00:06 2016-08-09 12:00:06 127.0.0.1 
2 1 2016-08-08 07:00:00 2016-08-08 13:00:00 127.0.0.1 
3 1 2016-08-08 13:10:00 2016-08-08 15:05:00 127.0.0.1 

因此,在这种情况下,我的输出会是这样的:

[ 
    0 => ["date" => "2016-08-09", "hours" => 9.00, "ipaddress" =>  "127.0.0.1"], 
    1 => ["date" => "2016-08-09", "hours" => 1.45, "ipaddress" => "127.0.0.1"] 
] 

的时间间隔取决于启动和停止都是类型的DateTime

我试过使用doctrine-extensions:oro/doctrine-extensions但是现在我得到异常错误:

[2/2] QueryException: [Syntax Error] line 0, col 50: Error: Expected Unit is not valid for TIMESTAMPDIFF function. Supported units are: "MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR", got 'stop' 

我的仓库方法是这样的:

public function findByToday() 
{ 
    $fields = array(
     'DATE(stop) as stop', 
     'TIME(SUM(TIMESTAMPDIFF(stop, start))) AS tdiff', 
     'device_id', 
     'ipaddress' 
    ); 

    $q = $this->createQueryBuilder('t') 
     ->select($fields) 
     ->where('DATE(stop) = :today') 
     ->setParameter('today', date('Y-m-d', time())) 
     ->groupBy('device_id') 
     ->getQuery(); 

    return $q->getResult(); 

} 

我的数据库表:

id device_id start stop ipaddress 
5 1 2016-08-09 09:00:06 2016-08-09 12:00:06 127.0.0.1 
6 1 2016-08-08 07:00:00 2016-08-08 13:00:00 127.0.0.1 
7 1 2016-08-08 13:10:00 2016-08-08 15:05:00 127.0.0.1 

BTW我使用的Symfony 3,可能是这个问题?

回答

0

根据您上面的表格(如果我猜中了)我想尝试直接与MYSQL获取数据,然后工作,我的方式来学说。

如果你有如下表:

CREATE TABLE `testdevices` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `device` INT(11) NULL DEFAULT NULL, 
    `dstart` DATETIME NULL DEFAULT NULL, 
    `dend` DATETIME NULL DEFAULT NULL, 
    `ip` TINYTEXT NULL, 
    PRIMARY KEY (`id`) 
); 

并通过下面的测试数据填充(我已经创建了两个设备,第二个是为精确3小时方便地活跃在今天的日期):

mysql> select * from testdevices; 
+----+--------+---------------------+---------------------+-----------+ 
| id | device | dstart    | dend    | ip  | 
+----+--------+---------------------+---------------------+-----------+ 
| 1 |  1 | 2016-08-09 09:00:06 | 2016-08-09 12:00:06 | 127.0.0.1 | 
| 2 |  1 | 2016-08-08 07:00:00 | 2016-08-08 13:00:00 | 127.0.0.1 | 
| 3 |  1 | 2016-08-08 13:10:00 | 2016-08-11 22:14:46 | 127.0.0.1 | 
| 4 |  2 | 2016-08-11 13:00:00 | 2016-08-11 14:00:00 | 127.0.0.1 | 
| 5 |  2 | 2016-08-11 15:00:00 | 2016-08-11 16:00:00 | 127.0.0.1 | 
| 6 |  2 | 2016-08-11 17:00:00 | 2016-08-11 18:00:00 | 127.0.0.1 | 
+----+--------+---------------------+---------------------+-----------+ 
6 rows in set (0.00 sec) 

下面的MySQL查询会那么相信将数据输出你想要的:

SELECT DATE(dend) as dend, TIME(SUM(TIMEDIFF(dend, dstart))) AS tdiff, device, ip 
FROM testdevices WHERE DATE(dend) = '2016-08-11' 
GROUP BY device ; 

结果将如下所示:

+------------+----------+--------+-----------+ 
| dend  | tdiff | device | ip  | 
+------------+----------+--------+-----------+ 
| 2016-08-11 | 81:04:46 |  1 | 127.0.0.1 | 
| 2016-08-11 | 03:00:00 |  2 | 127.0.0.1 | 
+------------+----------+--------+-----------+ 
2 rows in set (0.01 sec) 

请注意,第二个设备时间是正确的。现在,剩下的问题是如何做到这一点的学说。据我所知TIMEDIFF功能不是学说的一部分,又是那么我会建议2种不同的方法:

  1. 自己写的功能,如 http://www.doctrine-project.org/2010/03/29/doctrine2-custom-dql-udfs.html#date-diff(有例子DATEDIFF在这个环节,因此很容易可调),或
  2. 使用https://packagist.org/packages/oro/doctrine-extensions

教义/ Symfony2中你的最终处置库函数安装会是这个样子奥罗/学说的扩展:

$fields = array(
      'DATE(dend) as dend', 
      'SUM(TIMESTAMPDIFF(SECOND, dend, dstart)) AS tdiff', 
      'device', 
      'ip' 
     ); 

public function findFromToday() 
{ 
    $q = $this->createQueryBuilder('t') 
      ->select($fields) 
      ->where('DATE(dend) = :today') 
      ->setParameter('today', date('Y-m-d', time())) 
      ->groupBy('device') 
      ->getQuery(); 

    return $q->getResult(); 

我写了这个函数,根本不用测试它,从我的头顶开始。你可能会发现它失败了。由于TIMESTAMPDIFF需要指定单位,因此这会产生几秒钟的时间差。 安装奥罗/学说的扩展与

composer require oro/doctrine-extensions 
+0

托米斯拉夫感谢您的答复,我要去尝试一下,会让你知道如何去 –

+0

嗨,我已经尝试过了,但仍然没有工作,现在我获取预期错误:[2/2] QueryException:[语法错误]第0行,第50列:错误:预期单位对于TIMESTAMPDIFF函数无效。支持的单位是:“MICROSECOND,SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER,YEAR”,得到'停止' –

+0

在这里复制你的代码。 –