【问题标题】:Symfony - set entity field as timestampSymfony - 将实体字段设置为时间戳
【发布时间】:2021-01-27 13:45:16
【问题描述】:

在我的 Symofny 项目中,我希望我的实体拥有 timestamp 字段。

/**
 * @ORM\Column(type="datetime", nullable=false)
 */
private $timestamp;

/**
 * @ORM\PreUpdate
 * @throws \Exception
 */
public function setTimestamp()
{
    $this->timestamp = new DateTime("now");
    return $this;
}

我想以时间戳格式保存?我怎样才能做到这一点? 喜欢 // => 1387909800

我使用的是 Symfony 4.3 版本。

【问题讨论】:

  • 为什么要将它保存为时间戳?如果需要时间戳,可以从DateTime 对象轻松获取。

标签: php symfony timestamp


【解决方案1】:

试试getTimestamp();

/**
 * @ORM\Column(type="datetime", nullable=false)
 */
private $timestamp;

/**
 * @ORM\PreUpdate
 * @throws \Exception
 */
public function setTimestamp()
{
    $date = new \DateTime();
    $this->timestamp = $date->getTimestamp();
    return $this;
}

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 2017-06-19
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多