I'm new to Symfony2 (or Symfony3) and I can't find how to set doctrine (with annotations config) to automatically save it in my entities when 'created' or 'modified' fields.
我是Symfony2(或Symfony3)的新手,我无法找到如何设置doctrine(带注释配置),以便在“创建”或“修改”字段时自动将其保存在我的实体中。
5 个解决方案
#1
37
Here my solution after this time ...
在这之后我的解决方案......
You just need to put this directly into your entity class :
你只需要将它直接放入你的实体类:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class MyEntity {
//....
public function __construct() {
// we set up "created"+"modified"
$this->setCreated(new \DateTime());
if ($this->getModified() == null) {
$this->setModified(new \DateTime());
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function updateModifiedDatetime() {
// update the modified time
$this->setModified(new \DateTime());
}
//....
}
It works well actually
它实际上运作良好
#2
23
You can use StofDoctrineExtensionsBundle. This describes in symfony cookbook. It contains Timestampable behavior.
您可以使用StofDoctrineExtensionsBundle。这在symfony cookbook中有所描述。它包含Timestampable行为。
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
#3
9
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setModifiedAt(new \DateTime(date('Y-m-d H:i:s')));
if($this->getCreatedAt() == null)
{
$this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
}
}
You dont need to call in __constructor
anything. Just create getter
and setter
properties created
, modified
and that is all.
你不需要在__constructor中调用任何东西。只需创建创建,修改的getter和setter属性即可。
If you set first setCreated()
on every update you will update also created
colum. So put first setModifedAt()
如果您在每次更新时设置第一个setCreated(),您将更新也创建colum。所以把第一个setModifedAt()
#4
5
Two more examples (if you're using Yaml or Xml mapping):
还有两个例子(如果您使用的是Yaml或Xml映射):
Entity\Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 32
created_at:
type: date
gedmo:
timestampable:
on: create
updated_at:
type: datetime
gedmo:
timestampable:
on: update
And xml:
和xml:
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Mapping\Fixture\Xml\Timestampable" table="timestampables">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="created_at" type="datetime">
<gedmo:timestampable on="create"/>
</field>
<field name="updated_at" type="datetime">
<gedmo:timestampable on="update"/>
</field>
</entity>
</doctrine-mapping>
#5
1
The other answers suggest using if
statements (which means repeating your property names) and having property-setting logic in the constructor that might never be used.
其他答案建议使用if语句(这意味着重复您的属性名称)并在构造函数中具有可能永远不会使用的属性设置逻辑。
Alternatively, you could have onAdd
and onUpdate
methods that are called when needed:
或者,您可以使用在需要时调用的onAdd和onUpdate方法:
/**
* @ORM\PrePersist
*/
public function onAdd()
{
$this->setAdded(new DateTime('now'));
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function onUpdate()
{
$this->setUpdated(new DateTime('now'));
}
#1
37
Here my solution after this time ...
在这之后我的解决方案......
You just need to put this directly into your entity class :
你只需要将它直接放入你的实体类:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class MyEntity {
//....
public function __construct() {
// we set up "created"+"modified"
$this->setCreated(new \DateTime());
if ($this->getModified() == null) {
$this->setModified(new \DateTime());
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function updateModifiedDatetime() {
// update the modified time
$this->setModified(new \DateTime());
}
//....
}
It works well actually
它实际上运作良好
#2
23
You can use StofDoctrineExtensionsBundle. This describes in symfony cookbook. It contains Timestampable behavior.
您可以使用StofDoctrineExtensionsBundle。这在symfony cookbook中有所描述。它包含Timestampable行为。
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
#3
9
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setModifiedAt(new \DateTime(date('Y-m-d H:i:s')));
if($this->getCreatedAt() == null)
{
$this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
}
}
You dont need to call in __constructor
anything. Just create getter
and setter
properties created
, modified
and that is all.
你不需要在__constructor中调用任何东西。只需创建创建,修改的getter和setter属性即可。
If you set first setCreated()
on every update you will update also created
colum. So put first setModifedAt()
如果您在每次更新时设置第一个setCreated(),您将更新也创建colum。所以把第一个setModifedAt()
#4
5
Two more examples (if you're using Yaml or Xml mapping):
还有两个例子(如果您使用的是Yaml或Xml映射):
Entity\Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 32
created_at:
type: date
gedmo:
timestampable:
on: create
updated_at:
type: datetime
gedmo:
timestampable:
on: update
And xml:
和xml:
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Mapping\Fixture\Xml\Timestampable" table="timestampables">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="created_at" type="datetime">
<gedmo:timestampable on="create"/>
</field>
<field name="updated_at" type="datetime">
<gedmo:timestampable on="update"/>
</field>
</entity>
</doctrine-mapping>
#5
1
The other answers suggest using if
statements (which means repeating your property names) and having property-setting logic in the constructor that might never be used.
其他答案建议使用if语句(这意味着重复您的属性名称)并在构造函数中具有可能永远不会使用的属性设置逻辑。
Alternatively, you could have onAdd
and onUpdate
methods that are called when needed:
或者,您可以使用在需要时调用的onAdd和onUpdate方法:
/**
* @ORM\PrePersist
*/
public function onAdd()
{
$this->setAdded(new DateTime('now'));
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function onUpdate()
{
$this->setUpdated(new DateTime('now'));
}