CakePHP 4 如何将日期字段 created_at 分配给创建
CakePHP 4 How to assign date field created_at to created
我有一个table叫产品。在产品 table 中,我有 2 个字段 created_at
和 update_at
。我不想将其更改为 created
和 modified
。
在不更改 table 字段名称的情况下,如何指定创建 created_at 字段以获取 cakephp 时间助手自动更新日期?
我在下面尝试过的实体
protected $_accessible = [
'created_at' => $this->created,
'updated_at' => $this->modified,
];
获取错误。
阅读手册。 https://book.cakephp.org/4/en/orm/behaviors/timestamp.html#basic-usage
If you need to modify fields with different names, or want to update additional timestamp fields on custom events you can use some additional configuration:
class OrdersTable extends Table
{
public function initialize(array $config): void
{
$this->addBehavior('Timestamp', [
'events' => [
'Model.beforeSave' => [
'created_at' => 'new',
'updated_at' => 'always',
],
'Orders.completed' => [
'completed_at' => 'always'
]
]
]);
}
}
我有一个table叫产品。在产品 table 中,我有 2 个字段 created_at
和 update_at
。我不想将其更改为 created
和 modified
。
在不更改 table 字段名称的情况下,如何指定创建 created_at 字段以获取 cakephp 时间助手自动更新日期?
我在下面尝试过的实体
protected $_accessible = [
'created_at' => $this->created,
'updated_at' => $this->modified,
];
获取错误。
阅读手册。 https://book.cakephp.org/4/en/orm/behaviors/timestamp.html#basic-usage
If you need to modify fields with different names, or want to update additional timestamp fields on custom events you can use some additional configuration:
class OrdersTable extends Table
{
public function initialize(array $config): void
{
$this->addBehavior('Timestamp', [
'events' => [
'Model.beforeSave' => [
'created_at' => 'new',
'updated_at' => 'always',
],
'Orders.completed' => [
'completed_at' => 'always'
]
]
]);
}
}