Nehal
Nehal

Reputation: 1022

Cakephp handle MySQL enum

I got an enum datatype field in MySQL table named "privacy" = enum("Public","Private","Custom"). I want to show this field on my edit view like,

<?php 
    echo $this->Form->input(
              "privacy",array(
                   'label'=>'',
                   'type'=>'select'
              )
         );
  ?>

I tried this link solution but found two major problems with it.

1.) I need to pass the available options to the view from controller and need to assign them to the field in view file.

2.) It pass array as Numeric index, so on form submit view will pass the value's index rather than it's value. So if I select privacy option as "Custom", view will pass as 2 which eventually not gonna saved in database, cause its enum and 2 is not a valid option.

I thought that cakePHP is smart enough to evaluate that the provided field is enum and it will fetch all the available options from database and handle that field on it's own.

Is there any option in cakephp which solves my issue or do I need to pass a hard coded associative array to my view?

Upvotes: 1

Views: 1561

Answers (1)

liyakat
liyakat

Reputation: 11853

here is some code which is very useful to you to select gender as enum datatype

in view file

<?php
                echo $form->input('gender', array(
                        'type'=>'select',
                        'options'=>array("male" => "Male", "female" => "Female")
                    )
                );

and for controller you just have to initialize

$this->data['Profile']['gender'] = $sel_gender['Profile']['gender'];

let me know if i can help you more...

Upvotes: 1

Related Questions