简体   繁体   English

教义,symfony2用于计算小时数的正确列类型

[英]Doctrine, symfony2 correct column type to use for calculating hours

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? 在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? 这些时间计算的最佳实践是什么?

I think that for you case need to use "datetime" type filed. 我认为对于您而言,需要使用“ datetime”类型进行归档。 You can convert DateTime object in different formats 您可以转换不同格式的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;
}

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. 值对象会更加复杂。

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. 将时间存储为分钟的好处是您可以直接进行数学相关的搜索( SUMAVG等),而不必分别调用每个对象并以这种方式将它们相加。

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.. 除了使用整数方法外,您还可以创建相同的“时间”值对象,该对象花费了几分钟的时间,然后从对象中获取时间。

/**
 * 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.. 然后在您的价值对象中。

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后,或者返回一个空的Time对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM