私のデータベースには、列挙値 ('G','Y','M') を持つことができる列 say agent があります。ここで、G= google Y= yahoo M= msn
データベースがそれを認識して保存できるように、関数からの出力をどのようにマップする必要がありますか?
I think the easiest way is to get the int representation of the enum value. Enums are numbers in the background, so it's usually easy to cast an enum member to a number and vice versa.
You can write a converter class for the best flexibility. You can process the enum value and generate a string, or get a string and return an enum member. You can do this in a simple if-then-else or switch-case structure.
If you are using .NET (C#), be aware that you can pass any number as a parameter even you specify an enum, so an else or a default branch is a must in case of conversion.
If you are using PHP, here is an example:
$myEnum = array ("red" => 1,"blue" => 2,"yellow" => 3);
$d = $myEnum["red"];
You will get 1 for red, 3 for yellow. If you want to use a string:
$myEnum = array ("Google" => "G","Yahoo" => "Y","MSN" => "M");
$d = $myEnum["Google"];
Cakeは列挙型をサポートしていないため、tinyint(2)フィールドとこのアプローチを使用することをお勧めします: http ://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
より速く、保守が容易で、(カスタム)ベイクテンプレートでも機能します。