annotated
annotated copied to clipboard
💡 Feature Request: Add `insertable` and `updatable` attributes
Currently, the cycle/annotated library doesn’t support insertable and updatable column attributes, which are useful for preventing specific fields from being included in INSERT or UPDATE queries (e.g., auto-incremented IDs, database-managed timestamps, or read-only fields).
In Doctrine, these are implemented as:
#[Column(insertable: false, updatable: false)]
private $id;
Proposal:
Add similar attributes to Cycle\Annotated’s Column
#[Column(insertable: false)] // Excludes from INSERT
#[Column(updatable: false)] // Excludes from UPDATE
Use Cases:
- Auto-increment fields that should not be manually inserted.
- Database-managed fields (e.g., created_at, updated_at).
- Read-only fields fetched from triggers or views.
Would this be feasible to implement? I’m happy to help with a PR if the maintainers agree on the approach.
What about GeneratedValue?
What about GeneratedValue?
#[GeneratedValue(onInsert: true)] did not work for me.
because the column in my mysql database is marked as dt_exec timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, I needed the ORM to ignore the insert and update, in the current scenario when I update the entity and it already has the dt_exec a new timestamp is not generated, because the ORM updates it with the date that is already in the column.
There is a documentation about GeneratedValue at https://cycle-orm.dev/docs, as I couldn't find it.
You are right, the ORM always inserts user values because they have hight priority on generated ones. The same will happen if you manually set the id value for an autoincremental column.
You can unset the value of the generated column in the Mapper before saving.
Alternatively, you can create your own implementation of Entity Behavior with an attribute like #[CleanBeforeInsert].
GeneratedValue is not documented in the docs yet.