0
votes

i've been playing around with CakePHP lately and ran into the following problem:

while using form helper to create views i've been doing the following to output a select

    echo $this->Form->input('fee', array(
        'empty' => '---',
        'options' => array(
        __('Yes'),
        __('No'))
    ));

i prepared the strings to be ready for i18n so thats why they are declared with __(' ')

Thus, it works perfectly - and its generating the following code :

<div class="input select">
<label for="GameFee">Fee</label>
<select name="data[Game][fee]" id="GameFee">
<option value="">---</option>
<option value="0">Yes</option>
<option value="1">No</option>
</select>
</div>

Yet cake does - as usually - take the IDs as value. How can i force cake to take the field description as value like

Yes

The "fee" field inside the table "games" consist of varchar(100)

Hope you can help :)

1

1 Answers

1
votes

I would not do that! As of now cake does not support enum out of the box. But there are plenty of options to workaround it - and some of them are usually way better than using enums in the first place.

In your case I wonder why you would want an enum here? Its clearly a boolean value (two states: 0 or 1, yes or no). So I would always just store this boolean value using tinyint1.

You should store the base information here (0 or 1) in the database at a low cost (nothing compared to a whole string) and low regression errors and only translate/interprete it on output (in your view then for instance).

If you really need more than two definite states, read http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/ for example. You can easily achieve what you want by sticking to your tinyint2 field here:

$this->Form->input('fee', array('options' => Model::fees()));

Anyway, to answer your question: If you really must save a string here, make sure it is part of the key or map it to it in the controller prior to saving.

'options' => array(
    __('Yes') => __('Yes'),
    __('No') => __('No'))