-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Summary
Introduce an opt-in mechanism for models to automatically manage created_at and updated_at timestamp fields (and optionally deleted_at for soft deletes), similar in spirit to ORM timestamp behavior in other frameworks. This should be configurable per model and work with the current model architecture planned under the Model refactor.
Motivation
Many applications depend on tracking when records are created or updated. Right now, models in Quantum do not automatically handle these fields — requiring boilerplate in migrations and manual updates on save. An opt-in timestamp feature will improve developer experience, encourage consistency, and reduce repetitive code across projects.
Design Requirements
1. Opt-In Timestamps Flag
- Support a property on the model (e.g., $timestamps = true | false) that controls automatic timestamping.
- Default value should be false to avoid breaking current behavior.
2. Automatic Field Management
- When $timestamps = true, update timestamp fields automatically:
- created_at on insert
- updated_at on insert and update
- If a deleted_at column exists and soft deletes are enabled, update it on soft delete.
3. Field Customization
- Allow customizing timestamp column names (optional), e.g.,
$createdAt = 'created_on'; $updatedAt = 'modified_on';
4. Database Compatibility
- Support both DateTime (SQL
DATETIME,TIMESTAMP) and integer timestamp columns (UNIX timestamps) based on model/db config.
5. Backward Compatibility
- Models without $timestamps should behave exactly as before.
- Timestamp logic should not be enabled by default to keep current behavior unchanged.
6. Core Integration
- Integrate timestamp logic into the existing model lifecycle (save/update hooks) without coupling too tightly with ORM internals.
API / Example Usage
namespace App\Models;
use Quantum\Mvc\DbModel;
class Post extends DbModel
{
public $table = 'posts';
// enable automatic timestamps
public $timestamps = true;
// optional custom column names
public $createdAt = 'created_at';
public $updatedAt = 'updated_at';
}
Expected Behavior
- On save():
- If node is new: set created_at and updated_at to now.
- If updating: set updated_at to now.
- On soft delete (if implemented): set deleted_at to now.