Comments (6)
Hi @lilHermit for this case, you'll need to create a custom strategy, because we use the const name without the prefix to put in the option value and the const value to show in the list. So we don't have any strategy to work as you want. I suggest you to create new strategy than this work around.
Let us know if you need some more help about this issue.
Thanks,
Peter Moraes.
from enum.
Hi @lilHermit, if you want to put the integer in the database you need to create the constants in this way.
const TYPE_0 = 'PLATFORM';
const TYPE_1 = 'USER';
const TYPE_2 = 'DEVICE';
it will show the list using the names and the values will be the numbers.
do you have any question yet?
from enum.
@pmoraes Yeah that doesn't help because I want the constants to be meaningful so I can use them in queries.
$query->conditions(['type' => SampleTable::TYPE_PLATFORM]);
I'll go with the hacky approach but if I need it again I might look at writing a "Strategy"
from enum.
But where is it hacky? That's how PHP constants work. We do not have atoms/symbols in PHP, sadly.
You can of course do:
const TYPE_PLATFORM = 'TYPE_PLATFORM';
const TYPE_USER = 'TYPE_USER';
const TYPE_DEVICE = 'TYPE_DEVICE;
... then 'type' => SampleTable::TYPE_PLATFORM
should work.
And then at some point do some po file translation, if required.
from enum.
@inoas I think disabling applicationRules and using array_flip is a bit hacky personally.
I want to have an integer in the db for query/index speed.
No problem thanks for your time.
from enum.
The solution was quite simple for integer constants
https://bitbucket.org/snippets/lilHermit/onaLe
It would be easy to add a config option to the ConstStrategy for integers, which adds this array_flip
if other users request it.
Also as the validation callback uses the enum
method this fixes the Application rules issue too.
from enum.
Related Issues (19)
- I can contribute to the docs for this. Just need to learn how to do so HOT 4
- Enum Helper HOT 4
- Enum translation HOT 4
- Missing Helper? HOT 4
- Enums not working with irregular fieldnames HOT 3
- Create a way to reuse the const in some query conditions. HOT 7
- Add a config option to inject the enum validation into a validation object
- Improve default validation error
- Problem with multi worded prefix in ConstStrategy. HOT 2
- CakePHP 3.6 Update HOT 2
- Deprecated Error in Table class at CakePHP3.6 HOT 1
- CakePHP 4 support HOT 1
- ConstStrategy::enum() should cache generated lists.
- Error in ConstStrategy while formatig results HOT 2
- Error in ConstStrategy while formatting results HOT 1
- Error in Branch 3.next-cake5 in ConstStrategy HOT 3
- Problem modifying existing entity with enum and saving it
- Setup packagist hook. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enum.