I am recording peoples hours and minutes worked, and then adding them together to produce a total amount worked for a certain period. What is the best column type to use for this in Doctrine? My initial thought was just to use integer, but this will require back and forth conversion into Time formats.
我在记录人们的工作时间和工作时间,然后把他们加在一起,在一定的时间内完成工作。什么是最好的列类型用于这个教条?我最初的想法只是使用整数,但这需要来回转换为时间格式。
What is the best practice for these sorts of time calculations?
这种时间计算的最佳实践是什么?
2 个解决方案
#1
1
I would recommend either storing as an integer of minutes or as a custom "time" field that would automatically create a "Time" value object that would give you access to the methods that you need. The value object would be a bit more complicated.
我建议将存储作为分钟的整数存储,或者作为自定义的“time”字段存储,该字段将自动创建一个“time”值对象,使您能够访问所需的方法。value对象会稍微复杂一点。
The benefit of storing as minutes would be that you could do maths related searches (SUM
, AVG
, etc) directly rather than having to call each object individually and add them up that way.
存储为分钟的好处是,您可以直接执行与数学相关的搜索(SUM、AVG等),而不必逐个调用每个对象并以这种方式将它们相加。
On top of using the integer method you could create the same "Time" value object that took the time in minutes and then get that from your object like..
在使用integer方法的基础上,您可以创建相同的“Time”值对象,该对象在分钟内花费时间,然后从您的对象…
/**
* Get minutes worked
*
* @return integer
*/
public function getMinutes()
{
return $minutes;
}
/**
* Set working minutes
*
* @param integer $minutes
* @return $this
*/
public functions setMinutes($minutes)
{
$this->minutes = $minutes
return $this;
}
/**
* Get worked Time
*
* @return Time
*/
public function getTime()
{
if (null === $this->minutes) {
return $minutes;
// Or return Time::fromMinutes(0);
}
return Time::fromMinutes($this->minutes);
}
And then in your value object..
然后在value对象中。
class Time
{
private $minutes;
/**
* Private constructor so as to use more meaningful static calls
*
* @param integer $minutes
*/
private function __construct($minutes)
{
if ($is_int($minutes)) {
throw new \Exception(sprintf(
'Minutes expected to be an "integer", "%s" given',
gettype($minutes)
));
}
$this->minutes = $minutes;
}
/**
* Construct object from minutes
*
* @param integer $minutes
* @return Time
*/
public static function fromMinutes($minutes)
{
return self($minutes);
}
/**
* Get time in minutes
*
* @return integer
*/
public function getMinutes()
{
return $this->minutes;
}
/**
* Get time in hours and minutes string in 00:00 format
*
* @return string
*/
public function getAsString()
{
return sprintf(
'%02d:%02d',
floor($this->minutes / 60),
$this->minutes % 60
);
}
//...
Any other methods
}
So your form could just take the minutes (or a 00:00 string with a data transformer) and then you could call it like $job->getTime()->getAsString()
(after checking the time was not null, or alternatively returning an empty Time object).
因此,您的表单可以只占用分钟(或者使用数据转换器的00:00字符串),然后可以将其调用为$job—>getTime()—>getAsString()(在检查时间不是null之后,或者返回一个空时间对象)。
#2
1
I think that for you case need to use "datetime" type filed. You can convert DateTime object in different formats
我认为对于您的情况需要使用“datetime”类型文件。您可以以不同的格式转换DateTime对象。
/**
* @var \DateTime $start
*
* @ORM\Column(type="datetime")
*/
protected $start;
/**
* @var \DateTime $end
*
* @ORM\Column(type="datetime")
*/
protected $end;
/**
* @return \DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* @param \DateTime $start
*/
public function setStart(\DateTime $start)
{
$this->start = $start;
}
/**
* @return \DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* @param \DateTime $end
*/
public function setEnd(\DateTime $end)
{
$this->end = $end;
}
#1
1
I would recommend either storing as an integer of minutes or as a custom "time" field that would automatically create a "Time" value object that would give you access to the methods that you need. The value object would be a bit more complicated.
我建议将存储作为分钟的整数存储,或者作为自定义的“time”字段存储,该字段将自动创建一个“time”值对象,使您能够访问所需的方法。value对象会稍微复杂一点。
The benefit of storing as minutes would be that you could do maths related searches (SUM
, AVG
, etc) directly rather than having to call each object individually and add them up that way.
存储为分钟的好处是,您可以直接执行与数学相关的搜索(SUM、AVG等),而不必逐个调用每个对象并以这种方式将它们相加。
On top of using the integer method you could create the same "Time" value object that took the time in minutes and then get that from your object like..
在使用integer方法的基础上,您可以创建相同的“Time”值对象,该对象在分钟内花费时间,然后从您的对象…
/**
* Get minutes worked
*
* @return integer
*/
public function getMinutes()
{
return $minutes;
}
/**
* Set working minutes
*
* @param integer $minutes
* @return $this
*/
public functions setMinutes($minutes)
{
$this->minutes = $minutes
return $this;
}
/**
* Get worked Time
*
* @return Time
*/
public function getTime()
{
if (null === $this->minutes) {
return $minutes;
// Or return Time::fromMinutes(0);
}
return Time::fromMinutes($this->minutes);
}
And then in your value object..
然后在value对象中。
class Time
{
private $minutes;
/**
* Private constructor so as to use more meaningful static calls
*
* @param integer $minutes
*/
private function __construct($minutes)
{
if ($is_int($minutes)) {
throw new \Exception(sprintf(
'Minutes expected to be an "integer", "%s" given',
gettype($minutes)
));
}
$this->minutes = $minutes;
}
/**
* Construct object from minutes
*
* @param integer $minutes
* @return Time
*/
public static function fromMinutes($minutes)
{
return self($minutes);
}
/**
* Get time in minutes
*
* @return integer
*/
public function getMinutes()
{
return $this->minutes;
}
/**
* Get time in hours and minutes string in 00:00 format
*
* @return string
*/
public function getAsString()
{
return sprintf(
'%02d:%02d',
floor($this->minutes / 60),
$this->minutes % 60
);
}
//...
Any other methods
}
So your form could just take the minutes (or a 00:00 string with a data transformer) and then you could call it like $job->getTime()->getAsString()
(after checking the time was not null, or alternatively returning an empty Time object).
因此,您的表单可以只占用分钟(或者使用数据转换器的00:00字符串),然后可以将其调用为$job—>getTime()—>getAsString()(在检查时间不是null之后,或者返回一个空时间对象)。
#2
1
I think that for you case need to use "datetime" type filed. You can convert DateTime object in different formats
我认为对于您的情况需要使用“datetime”类型文件。您可以以不同的格式转换DateTime对象。
/**
* @var \DateTime $start
*
* @ORM\Column(type="datetime")
*/
protected $start;
/**
* @var \DateTime $end
*
* @ORM\Column(type="datetime")
*/
protected $end;
/**
* @return \DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* @param \DateTime $start
*/
public function setStart(\DateTime $start)
{
$this->start = $start;
}
/**
* @return \DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* @param \DateTime $end
*/
public function setEnd(\DateTime $end)
{
$this->end = $end;
}