Introduction

ORM Model - Time Fields - 图1warning

Note that this feature is only effective for chaining operations.

The gdb module supports the automatic filling of creation, update, and deletion times for data records, improving development and maintenance efficiency. To facilitate unified maintenance of time field names and types, if using this feature, we have the following conventions:

  • The field type can be a time type, numeric integer, or boolean, such as: date, datetime, timestamp, int, uint, big int, bool, etc.
  • Field names can be customized, with default naming conventions being:
    • created_at is updated during record creation and is only written once.
    • updated_at is updated during record modification and is updated each time the record changes.
    • deleted_at is for the soft delete feature of the record and is only written once when the record is deleted. Field names are case-insensitive and ignore special characters, for example, CreatedAt, UpdatedAt, DeletedAt are also supported.

Feature Configuration

Time field names can be customized in the configuration file, and the feature can be completely disabled on the database instance using the timeMaintainDisabled configuration.

Corresponding configuration items in the configuration file:

  1. database:
  2. default: # Group name, customizable, default is "default"
  3. createdAt: "created_at" # (Optional) Auto-create time field name
  4. updatedAt: "updated_time" # (Optional) Auto-update time field name
  5. deletedAt: "is_deleted" # (Optional) Soft delete time field name
  6. timeMaintainDisabled: false # (Optional) Whether to completely disable the time update feature. If true, CreatedAt/UpdatedAt/DeletedAt will be ineffective

ORM Model - Time Fields - 图2tip

Especially for historical projects, where different time field names already exist, the time field names can be flexibly configured using configuration items.

For a complete database configuration, please refer to the ORM - Configuration section.

Feature Activation

When a data table contains any or multiple fields such as created_at, updated_at, deleted_at, or contains corresponding configuration fields in the configuration file, this feature is automatically enabled.

Documentation

📄️ Time Fields - IntroThis article introduces the basic methods for managing database time fields using the GoFrame framework, including the mechanisms for writing and updating fields like created_at, updated_at, and deleted_at, and the impact of soft delete features on query and update operations. It also demonstrates methods for join queries and ignoring time features with Unscoped. Through these examples, you can effectively manage soft deletion and timestamps, ensuring the accuracy of database records.

📄️ Time Fields - Integer FieldsIf time fields such as created_at, updated_at, and deleted_at are integer fields, GoFrame’s ORM component will automatically recognize and write second-level timestamps. During insertion, created_at is automatically updated, but updates and deletions do not change created_at. The Replace method will update all time fields. In the case of soft deletion, all queries automatically include the condition deleted_at=0.

📄️ Time Fields - Boolean FieldsIntroduces support for Boolean fields in time fields of the GoFrame framework, demonstrating through examples how to use a Boolean type ‘deleted_at’ field for data soft deletion. Provides MySQL table structure definition and examples of creating records and performing soft delete operations using the ORM component in GoFrame.

📄️ Time Fields - SoftTimeOptionIntroduction on how to use SoftTimeOption in the GoFrame framework to control time writing granularity, converting from second-level to millisecond-level timestamps, and providing relevant MySQL table structures and example code to help developers flexibly configure time fields, supporting multiple time granularity options to meet different project needs, and inserting data through ORM methods