Cakephp - Convert numerical DB value into string for display
I have a Classroom model, which has an INT field called level. The values for this field can range from 1-7. What I would like to do is translate the integer in that field into a String for display, according to a predetermined legend, such as this:
1 - Basic I
2 - Basic I开发者_JAVA技巧I
3 - Basic III
4 - Intermediate I
etc... What would be the best way of achieving this? Using afterFind()
?
Thanks
Why not make an extra table containing strings matching the ID of the INT field called level. Get the string from that table using the matching ID and your good to go.
the quickest way (if the strings do not change much!):
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
echo Classroom::levels($classroom['Classroom']['level']);
in the view - whereever you need it
it also uses less resources than any other approach (therefor is the fastest).
精彩评论