0

I have a table called products. In product table I have 2 fields created_at and update_at. I don't want to change it to created and modified.

Without change table fields name, how can I assign that created_at field is created to get cakephp time helper auto update date ?

My entity I have tried below

protected $_accessible = [
        'created_at' => $this->created,
        'updated_at' => $this->modified,
];

Getting error.

Niloy Rony
  • 602
  • 1
  • 8
  • 23

1 Answers1

4

Read the manual. 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'
                ]
            ]
        ]);
    }
}
floriank
  • 25,546
  • 9
  • 42
  • 66