我想使字段updated_at
和created_at
我的Doctrine实体自动更新。
在Ruby on Rails模型中,有2个字段:updated_at
和created_at
。
可以在以下位置找到描述:http : //guides.rubyonrails.org/migrations.html#migration-overview:
时间戳宏添加了两列,created_at和updated_at。这些特殊列(如果存在)由Active Record自动管理。
我可以在Doctrine 2中启用类似功能吗?
- 你可以调用
$this->setCreatedAt(new \DateTime())
的__construct
方法。 - 您可以使用生命周期回调
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() === null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
并且不要忘了添加到实体类符号中:
@ORM\HasLifecycleCallbacks
如果您想分别处理它们,这是另一个选择。
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="person")
* @ORM\HasLifecycleCallbacks
*/
class Person
{
..........
/**
* @var datetime $created
*
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @var datetime $updated
*
* @ORM\Column(type="datetime", nullable = true)
*/
protected $updated;
/**
* Gets triggered only on insert
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->created = new \DateTime("now");
}
/**
* Gets triggered every time on update
* @ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updated = new \DateTime("now");
}
..........
}
对我来说,最方便的解决方案是StofDoctrineExtensionsBundle的Timestampable
功能。
简单的配置,以后你能够做出领域createdAt
和updatedAt
中Entity
填写了加入两个简单的自动annotations
,如:
@Gedmo\Mapping\Annotation\Timestampable(on="create")
和/或
@Gedmo\Mapping\Annotation\Timestampable(on="update")
例如
/**
* @var \DateTime
* @Gedmo\Mapping\Annotation\Timestampable(on="create")
* @Doctrine\ORM\Mapping\Column(type="datetime")
*/
protected $createdAt;
/**
* @var \DateTime
* @Gedmo\Mapping\Annotation\Timestampable(on="update")
* @Doctrine\ORM\Mapping\Column(type="datetime")
*/
protected $updatedAt;
没有任何多余的纯代码PHP
。
您也可以将其实现为特征-像这样:
<?php
namespace App\Entity\Traits;
use DateTime;
use DateTimeInterface;
use Exception;
/**
* Trait TimeStampableTrait
* @package App\Entity\Trait
*/
trait TimeStampableTrait
{
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @return DateTimeInterface|null
* @throws Exception
*/
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt ?? new DateTime();
}
/**
* @param DateTimeInterface $createdAt
* @return $this
*/
public function setCreatedAt(DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt ?? new DateTime();
}
/**
* @param DateTimeInterface $updatedAt
* @return $this
*/
public function setUpdatedAt(DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function updateTimestamps(): void
{
$now = new DateTime();
$this->setUpdatedAt($now);
if ($this->getId() === null) {
$this->setCreatedAt($now);
}
}
}
将此特征添加到您的实体中(不要忘了@ORM\HasLifecycleCallbacks()
符号):
<?php
namespace App\Entity;
use App\Entity\Traits\TimeStampableTrait;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MyEntityRepository")
* @ORM\HasLifecycleCallbacks()
*/
class MyEntity
{
use TimeStampableTrait;
}
我建议使用时间戳特征
https://symfonycasts.com/screencast/symfony4-doctrine/timestampable
use Gedmo\Timestampable\Traits\TimestampableEntity;
class Article
{
use TimestampableEntity;
}
将自动添加所有适当的功能
本文地址:http://php.askforanswer.com/zaidoctrinezhongupdated_atcreated_atdezidongzhi.html
文章标签:doctrine-orm , php
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:doctrine-orm , php
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!