cakephp / bake

The Bake Command Plugin
Other
110 stars 100 forks source link

Allow baking enums. #968

Closed dereuromark closed 8 months ago

dereuromark commented 9 months ago

bin/cake bake enum FooBar

or

bin/cake bake enum FooBar -i

Next step could be to use conventions to auto map/detect enums for table fields:

E.g. Users table with field status: User entity and UserStatus enum would map and get added to forms etc.


the fails are unrelated and will be green once https://github.com/cakephp/cakephp/pull/17482 is released with 5.0.4

dereuromark commented 8 months ago

Given the description I think --backed=string|int should suffice as option.

$ bin/cake bake enum FooBar -h
Bake (backed) enums for use in models.

Usage:
cake bake enum [options]

Options:

--backed, -b      If using backed enums. Set to `string` or `int`.
--connection, -c  The datasource connection to get data from.
dereuromark commented 8 months ago

In a 2nd step we could think of allowing argument/option to dictate the enum values, similar to bake migrations etc:

Backed int

0: inactive, 1:active, ...

Backed string

nope: inactive, 'yep yep':active, ...

Default

inactive, active, ...

In a 3rd step: If those would be part of the db field comment, it could be used to auto-generate the whole enum based on that, making it super RAD.

dereuromark commented 8 months ago

Looks like we can bake enums now for xmas:)

dereuromark commented 8 months ago

Given that we only now have backed, and string vs int Couldnt we just add boolean --int flag? -b ... seems not necessary in that case.

ADmad commented 8 months ago

--int has just 1 character less than -b int :)

I would prefer if we keep the more descriptive name.

dereuromark commented 8 months ago

Should it be just type/t though? Since it is clear it is a backing type on backed enums :)

ADmad commented 8 months ago

I can live with type if others are in favor.

dereuromark commented 8 months ago

Never mind, t is theme :)

That said: Shouldnt it throw an error if you define a short twice?

--theme, -t       The theme to use when baking code. (choices:
                  Bake|BootstrapUI|Migrations|Queue|Search|Setup|StateMachine)
--type, -t        The return type for the backed enum class

No error here..

ADmad commented 8 months ago

Shouldnt it throw an error if you define a short twice?

Guess we are missing duplicate naming checks.

dereuromark commented 8 months ago

@ADmad it is now simpler, only one char for int

So all good?