Welcome Guest, Not a member yet? Register   Sign In
Enum field type not supported ? - $this->db->field_data('table_name');
#1

[eluser]Brad Martin[/eluser]
Hi Guys,

Trying to get a list of a specific tables fields (Database is MYSQL) and their types.

One field is an ENUM field, when codeigniter returns the fields and their types it lists this particular field type as a string with its $field->max_length as 8 which is the length of the largest option.

Just wondering if their is a specific reason why codeigniter does or is it a bug of some kind ?

Thanks
#2

[eluser]InsiteFX[/eluser]
It's doe's not handle he enum type, I just had to write a create function because dbforge would not handle it either! This needs to be updated in the database...

InsiteFX
#3

[eluser]Brad Martin[/eluser]
Thanks, I got it working how i needed to modified the field_data() function in ./system/database/DB_driver.php

to call the enum_select() function from (http://ellislab.com/forums/viewthread/101110/) that i also added to DB_driver.php

Just thought i would post my fix for anyone else that may need it. I do realise this probably isn't the best way but it works! also adds unnecessary database calls if you don't need to check if the type is enum for each field.

Code:
function field_data($table = '')
    {
        if ($table == '')
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_field_param_missing');
            }
            return FALSE;
        }
        $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
        $rebuild = array();
        $fields = $query->field_data();
        if($fields){
            $count = 0;
            foreach($fields as $field){
                $el = @$this->enum_select($table,$field->name);
                if($el){
                    $fields[$count]->type = 'enum';
                    $fields[$count]->possible_values = $el;
                }
                $count++;
            }
            return $fields;
        }else{
            return $query->field_data();
        }
    }
Code:
function enum_select( $table , $field )
    {
        $query = "SHOW COLUMNS FROM ".$table." LIKE '$field'";
        $row = $this->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;
        $regex = "/'(.*?)'/";
        preg_match_all( $regex , $row, $enum_array );
        $enum_fields = $enum_array[1];
        foreach ($enum_fields as $key=>$value){
            $enums[$value] = $value;
        }
        return $enums;
    }
#4

[eluser]InsiteFX[/eluser]
Thanks! But now you have the problem that it will be over-written with a new update of CodeIgniter!

Best to report it to the Reactor Team as an enhacement.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB