【问题标题】:Logic error with Datetime->modify in Symfony entity日期时间逻辑错误-> Symfony 实体中的修改
【发布时间】:2018-11-29 18:42:01
【问题描述】:

我有一个 Symfony 实体 Equipment() 。该实体代表一个设备......它具有控制和控制期(以天为单位)(例如,每个控制之间需要 30 天)。 一个控件有一个日期,我正在尝试显示下一个需要的控件日期。

这是我的设备实体:

<?php

namespace StockBundle\Entity;

use StockBundle\Entity\EquipmentControle;

/**
 * Equipment
 *
 * @ORM\Table(name="stock_equipment")
 * @ORM\Entity(repositoryClass="StockBundle\Repository\EquipmentRepository")
 */
class Equipment
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="EquipmentControle", mappedBy="equipment")
     */
    private $controles;

    /**
     * @var float
     *
     * @ORM\Column(name="control_interval", type="integer", options={"default" : 0})
     */
    private $controlInterval = 0;

    public function getControles()
    {
        return $this->controles;
    }

    public function getControlInterval()
    {
        return $this->controlInterval;
    }

    public function setControles($controles)
    {
        $this->controles = $controles;
    }

    public function setControlInterval($controlInterval)
    {
        $this->controlInterval = $controlInterval;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    public function getLastControlDate(){
        $lastControle = null;
        foreach ($this->getControles() as $c){
            if($lastControle == null || $c->getDate() > $lastControle->getDate()){
                $lastControle = $c;
            }
        }
        return $lastControle == null ? null : $lastControle->getDate();
    }

    public function getNextControlDate(){

        if($this->getLastControlDate() == null && $this->getControlInterval() > 0){
            return new \DateTime("NOW");
        }elseif ($this->getLastControlDate() != null && $this->getControlInterval() > 0){
            return $this->getLastControlDate()->modify('+'.$this->getControlInterval().'day');
        }else{
            return null;
        }

    }
}

我使用 twig 以这种方式显示我的上一个和下一个控制日期:

{% if e.lastControlDate is not null %}{{ e.lastControlDate|date('d/m/Y') }}{% else %}-{% endif %}

{% if e.nextControlDate is not null %}{{ e.nextControlDate|date('d/m/Y') }}{% else %}-{% endif %}

但似乎 nextControlDate 始终是假定日期的两倍。

我在 2018 年 11 月 29 日获得了最后一次控制权,在 10 获得了$controlInterval,并在 2018 年 12 月 19 日获得了下一次控制权。当我尝试更改 controlInterval 时也是如此。

当我在lastControlDate 之前显示nextControlDate 时,我得到控制lastControl 和nextControl 的相同日期...

有什么帮助吗?

【问题讨论】:

  • 这是因为您正在修改 DateTime 对象的同一个实例。查看 PHP 文档中的引用:php.net/manual/en/language.references.php 并尝试将 DateTime 对象更改为 DateTimeImmutable 或在修改之前克隆该对象
  • 我知道,但是下一个控件是在最后一个控件日期函数之后执行的。所以这不是问题。即使你是对的,使用 datetimeImmuable 将允许我以任何顺序使用这些函数......真正的问题是我的 nextControlDate 是应有的两倍。
  • 如果您将DateTime 更改为DateTimeImmutable 它开始工作,您会看到:) 为什么间隔添加了两次?因为您两次调用方法 nextControlDate ;) 首先是在树枝条件下,其次是在打印实际日期时。这就是为什么你应该尽可能使用不可变结构,以避免这些“神秘”问题;)
  • 哦,该死的,我刚刚明白了,非常感谢你,我明天先试试这个,但我相信它会起作用。 (我以前不知道 datetimeImmuable,所以感谢您的课程;))

标签: php symfony datetime logic entity


【解决方案1】:

使用不可变版本的 Datetime,这样您就可以多次使用它,而无需从上次修改中重新计算。

if ($this->getLastControlDate() != null && $this->getControlInterval() > 0){

    $dateImmuable = new \DateTimeImmutable();
    $dateImmuable->createFromMutable($this->getLastControlDate());

    return $dateImmuable->modify('+'.$this->getControlInterval().'day');
}   

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多