cycle / annotated

Schema generation using annotated entities and mappers
MIT License
23 stars 12 forks source link

Virtual generated columnšŸ’” #98

Open peldax opened 2 weeks ago

peldax commented 2 weeks ago

I have an idea!

Hi,

I would like to extend the functionality to support virtual generated columns.

In Doctrine it is possible using this explicit annotation:

#[ORM\Column(type: 'int', columnDefinition: 'INT GENERATED ALWAYS AS (IF(xyz IS TRUE, 100, 200)) VIRTUAL', generated: 'ALWAYS')]
private int $someNumber;

Possibly the freshly added GeneratedColumn attribute might be used to support the functionality?

I am open to bounty this feature.

roxblnfk commented 2 weeks ago

Hi. If we talk about GeneratedValue attribute, then this attribute tells ORM that field is generated on the DB side. Orm adds a generated field into returning columns when insert or update. We can extend that attribute in the new one or extract an interface.

However I think it will be better to make a new attribute for virtual columns. For example:

#[ORM\VirtualColumn(type: 'int', expression: 'IF(xyz IS TRUE, 100, 200)', stored: true, typecast: 'int')